简体   繁体   English

如何在单击鼠标和按住鼠标之间切换?

[英]How to switch between one click mouse and mouse hold down?

When it's one click i want the character to Walk to the mouse position. 一键单击时,我希望角色走到鼠标位置。 But if it's holding down the mouse button i want it to rotate the character. 但是,如果按住鼠标按钮,我希望它旋转角色。 But for some reason it's all the time on the Idle state. 但是由于某种原因,它始终处于空闲状态。 So when i make one click the character jump in small jump and not walking to the mouse position. 因此,当我一键单击时,角色会以较小的跳跃跳入,而不会移动到鼠标位置。

If i will remove the whole part of the if (Input.GetMouseButton(0)) then when clicking the mouse it will walk fine to the mouse position but once added this part he will not walk. 如果我将删除if(Input.GetMouseButton(0))的整个部分,则在单击鼠标时它将很好地移动到鼠标位置,但是一旦添加此部分,他将不会走路。

void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            _animator.CrossFade("Walk", 0);
            Plane playerPlane = new Plane(Vector3.up, transform.position);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float hitdist = 0.0f;

            if (playerPlane.Raycast(ray, out hitdist))
            {
                Vector3 targetPoint = ray.GetPoint(hitdist);
                targetPosition = ray.GetPoint(hitdist);
                targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                destination = targetPosition;
            }
        }

        if (Input.GetMouseButton(0))
        {
            _animator.CrossFade("Idle", 0);
            RaycastHit hit;
            Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray1, out hit) && hit.collider.name != "ThirdPersonController")
            {
                transform.LookAt(hit.point);
            }
            else
            {
                transform.LookAt(ray1.GetPoint(100));  //the number here is compltely arbitrary
            }
        }
     }

Input.GetMouseButton(int) returns true always if the mouse button is being pressed. 如果按下鼠标按钮, Input.GetMouseButton(int) 始终返回true。 Input.GetMouseButtonDown(int) returns true only on the first frame during which the mouse button is clicked. Input.GetMouseButtonDown(int) 在单击鼠标按钮的第一帧上返回true。

So given your code, the possibilities are: 因此,鉴于您的代码,可能性是:

  • No button is being pressed. 没有按钮被按下。 None of the if blocks are entered. 没有任何if块输入。
  • This is the exact frame when the mouse button was clicked. 这是单击鼠标按钮时的确切帧。 Both of the if blocks are executed. 两个if块均被执行。 Your animator state is set to Walk and then immediately to Idle and your character doesn't move. 您的动画师状态设置为“ Walk ,然后立即设置为“ Idle并且角色不会移动。
  • The mouse button was pressed down a few frames ago and is still being held down. 几帧之前已按下鼠标按钮,但仍处于按下状态。 Only the second if block is executed. 仅执行第二个if块。 Your animator state is set to Idle and the character doesn't move. 您的动画师状态设置为“ Idle ,角色不移动。

If you have decided to use this particular control scheme, you should consider implementing some sort of delay - maybe mouse button presses shorter than 0.2s should count as a click and presses longer than 0.2s should count as a hold . 如果您决定使用此特定的控制方案,则应考虑实现某种延迟-可能将少于0.2s的鼠标按键按下视为一次点击,而将超过0.2s的按下视为暂停 This way your predicates won't overlap. 这样,您的谓词就不会重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM