RigidBody用鼠标拖放

时间:2015-01-28 22:02:03

标签: unity3d

我一直试图拖拉一个僵硬的人。我用过这个脚本。它用鼠标移动刚体,但不会掉落刚体,它会继续移动。

public class move : MonoBehaviour {

public Camera cam;
private float maxWidth;

// Use this for initialization
void Start()
{
    if (cam == null)
    {
        cam = Camera.main;
    }
    Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f);
    Vector3 targetWidth = cam.ScreenToWorldPoint(upperCorner);
    float hatWidth = renderer.bounds.extents.x;
    maxWidth = targetWidth.x - hatWidth;
}


// Update is called once per frame
void FixedUpdate () {

    Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
    Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
    float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
    targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
    rigidbody.MovePosition(targetPosition);


}

}

我想要类似this示例的内容,这意味着它会拖动一个拥有刚体的游戏对象并放置到位。

由于

1 个答案:

答案 0 :(得分:0)

您需要使用Input.GetMouseButtonDown进行开始拖动,

Input.GetMouseButton用于拖动,

Input.GetMouseButtonUp用于结束拖动。

简单代码是:

void FixedUpdate () {
    // 0 means left mouse button.
    if(Input.GetMouseButton(0)){ // Check left button is still down so change rigidbody position
       Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
       Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
       float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
       targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
       rigidbody.MovePosition(targetPosition);
    }else if(Input.GetMouseButtonDown(0)){
       Vector3 startPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
    }else if(Input.GetMouseButtonUp(0)){
       cam.ScreenToWorldPoint(Input.mousePosition);
}

根据您的需要进行更新

 void FixedUpdate () {
        // 0 means left mouse button.
        if(Input.GetMouseButton(0)){ // Check left button is still down so change rigidbody position
           RaycastHit hit;
           Ray ray = cam.ScreenPointToRay(Input.mousePosition);
           if (Physics.Raycast(ray,out hit,100)){
               if(hit.collider.gameObject.name=="TargetObjectName"){
                  Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
                   //This is your solution 
                   Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
                   float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
                   targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);

                   // Maybe this is more usefull
                   Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
                   rigidbody.MovePosition(targetPosition);
               }
           }
        }else if(Input.GetMouseButtonDown(0)){
           Vector3 startPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
        }else if(Input.GetMouseButtonUp(0)){
           Vector3 endPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
    }

PS:也许,代码中的一些语法错误我没有检查它。