从球自身位置移动球Unity3D

从球自身位置移动球Unity3D,unity3d,Unity3d,我想用鼠标移动我的球,当前球跟随鼠标的位置,我想跟随鼠标的行为而不是位置 我试图用速度移动,但我用速度向前移动我的球,所以它的行为出乎意料 private void Update() { rigid.velocity = new Vector3(0f, -2f, Time.deltaTime * speed); if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointTo

我想用鼠标移动我的球,当前球跟随鼠标的位置,我想跟随鼠标的行为而不是位置

我试图用速度移动,但我用速度向前移动我的球,所以它的行为出乎意料

 private void Update()
  {
    rigid.velocity = new Vector3(0f, -2f, Time.deltaTime * speed);
      if (Input.GetMouseButton(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
          {
            Vector3 point = hit.point;

            rigid.MovePosition(new Vector3(Mathf.Lerp(transform.position.x,Mathf.Clamp(point.x, -3, 3),5 * Time.deltaTime), transform.position.y, transform.position.z));

        }
    }

    if (Input.touchCount > 1)
    {
        Touch touch = Input.GetTouch(0);
        switch (touch.phase)
        {
            case TouchPhase.Moved:
                rigid.MovePosition(new Vector3(Mathf.Lerp(transform.position.x, Mathf.Clamp(touch.position.x, -3, 3), 5 * Time.deltaTime), transform.position.y, Mathf.Lerp(transform.position.z, touch.position.y, 5 * Time.deltaTime)));
                break;
        }
    }
}

这个代码是根据鼠标移动球,但我不想跟随鼠标的位置。我只需要按照左右上下的方式进行操作。

我没有尝试过,但概念上是这样的:

Vector2 prevMousePos = Vector2.zero;
private float power = 10; // Change this to edit power

private void Update()
{
    if (Input.GetMouseButton(0))
    {
        Vector2 mousePos = Input.mousePosition;
        Vector2 direction = (mousePos - prevMousePos).normalized;
        rigid.AddForce(direction * power);
        prevMousePos = mousePos;
    }
}
请注意,如果您像您的代码当前所做的那样设置Velocity every
Update()
,它将覆盖所有其他物理运动。您可以通过将AddForce作为一个恒定的运动来解决这个问题,然后为了确保对象不会无限快,添加一个速度检查并限制它:

const float MAX_VELOCITY = 10;

void Update() 
{
    /* 
     * ... Input.GetMouseButton(0) ...
     */

    // Add force in the direction you today have set for rigid.velocity
    rigid.AddForce(new Vector3(0f, -2f, Time.deltaTime * speed));

    // Make sure the AddForce doesn't goes faster than MAX_VELOCITY
    if (rigid.velocity.magnitude > MAX_VELOCITY) 
    {
        rigid.velocity = rigid.velocity.normalized * MAX_VELOCITY;
    }
}


我没有试过,但概念上是这样的:

Vector2 prevMousePos = Vector2.zero;
private float power = 10; // Change this to edit power

private void Update()
{
    if (Input.GetMouseButton(0))
    {
        Vector2 mousePos = Input.mousePosition;
        Vector2 direction = (mousePos - prevMousePos).normalized;
        rigid.AddForce(direction * power);
        prevMousePos = mousePos;
    }
}
请注意,如果您像您的代码当前所做的那样设置Velocity every
Update()
,它将覆盖所有其他物理运动。您可以通过将AddForce作为一个恒定的运动来解决这个问题,然后为了确保对象不会无限快,添加一个速度检查并限制它:

const float MAX_VELOCITY = 10;

void Update() 
{
    /* 
     * ... Input.GetMouseButton(0) ...
     */

    // Add force in the direction you today have set for rigid.velocity
    rigid.AddForce(new Vector3(0f, -2f, Time.deltaTime * speed));

    // Make sure the AddForce doesn't goes faster than MAX_VELOCITY
    if (rigid.velocity.magnitude > MAX_VELOCITY) 
    {
        rigid.velocity = rigid.velocity.normalized * MAX_VELOCITY;
    }
}


我可能会用AddForce的方法来处理这个问题。在鼠标从前一帧移动到这一帧的矢量中向球添加力。我可能会使用AddForce方法来实现这一点。将力添加到向量中的球鼠标从前一帧移动到这一帧。每次我与块碰撞时,力都会下降,通过添加addforce,有一些块可以停止力,有很多块是背靠背的,所以加力不适合这种情况。然后你必须做类似的事情,但是改变速度。在概念上应该是同一件事;检查最后一帧和这一帧之间的鼠标位置差异,并将其添加到当前速度向量中。每次我与块碰撞时,通过添加addforce,会有一些块停止force,有很多块是背靠背的,所以加力不适合这种情况。然后你必须做类似的事情,但是改变速度。在概念上应该是同一件事;检查最后一帧和此帧之间的鼠标位置差,并将其添加到当前速度向量中。