【问题标题】:Move Ball left and right with mouse drag Unity 3D通过鼠标拖动 Unity 3D 左右移动球
【发布时间】:2021-12-07 21:05:17
【问题描述】:

我有一个连续弹跳的球,我想用鼠标在 x 轴上左右移动它,让它跟随鼠标的 X 移动。

我做了下面的脚本,但是球没有用鼠标移动!

球脚本:

private Vector3 pos;
public Camera cam;

public Rigidbody Ball;
public float Speed;
public static float GlobalGravity = -9.8f;
public float GravityScale = 1.0f;
bool isforce = false;

private void Start(){

    Ball = GetComponent<Rigidbody>();
    Ball.useGravity = false;

}


private void FixedUpdate(){

    Vector3 gravity = GlobalGravity * GravityScale * Vector3.up;
    Ball.AddForce(gravity, ForceMode.Acceleration);

}

void force (){

    isforce = false;
    Ball.AddForce(Vector3.up * Speed, ForceMode.Impulse);
}

private void Update(){

    if (isforce == true){

        force();
    }

    if (Input.GetMouseButton(1)){

        Vector3 mousePos = Input.mousePosition;
        Vector3 wantedPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, transform.position.y, 10));
        transform.position = wantedPos;
    }
}

private void OnCollisionEnter(Collision collision){

    isforce = true;
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    试试这个,让我知道会发生什么。您的设置为右键单击,但我将其更改为左键单击,除了我只是将球 x 放在鼠标输入的原始 x 上。

    private void Update(){
    
        if (isforce == true){
    
            force();
        }
    
        if (Input.GetMouseButton(0)){
    
            Vector3 mousePos = Input.mousePosition;
            Vector3 wantedPos = transform.position;
    wantedPos.x=Input.mousePosition.x;
            transform.position = wantedPos;
        }
    }
    

    【讨论】:

    • 当点击球远离屏幕时,X位置变为511而不是0!
    • 用您的原始代码尝试一下,然后右键单击,看看它是否适合您
    • 我试过了,但是它改变了它的Y位置,并且拖动时球停止弹跳
    • 有什么建议吗?
    【解决方案2】:

    当您使用 transform.position 时,Unity Physics 将不适用于刚体。找到您的鼠标相对于球的位置,并相应地在 X 方向上添加力。

    【讨论】:

      【解决方案3】:

      你有几个选择,这取决于你想要什么。

      正如@Hamed 回答的那样,在使用物理时,您不想直接更新变换,而是使用刚体添加力。

      相对于鼠标所在位置的力常数

      if (Input.GetMouseButton(0))
      {
          Vector2 force = Vector2.zero;
      
          //Get the Balls current screenX position
          float ballX = Camera.WorldToScreenPoint(Ball.transform.position).x;
      
          //Check if Click was Left or Right of the Ball
          if (ballX > Input.mousePosition.x)
          {
              //Click was Left of the Ball
              force = new Vector2(-1f, 0f);
          }
          else if (ballX < Input.mousePosition.x)
          {
              //Click was Right of the Ball
              force = new Vector2(1f, 0f);
          }
      
          //Adjust force amount to your suiting
          Ball.Addforce(force * Time.deltaTime);
      }
      

      相对于鼠标移动的力

      if (Input.GetMouseButton(0))
      {
          Vector2 force = Vector2.zero;
      
          float mouseDelta = Input.GetAxis("Mouse X");
      
          //Check if the Mouse is going Left or Right
          if (mouseDelta < 0f)
          {
              //Click was Left of the Ball
              force = new Vector2(-1f, 0f);
          }
          else if (mouseDelta > 0f)
          {
              //Click was Right of the Ball
              force = new Vector2(1f, 0f);
          }
      
          // Update force relative to mouse movement
          force = Mathf.Abs(mouseDelta) * force;
      
          //Adjust force amount to your suiting
          Ball.Addforce(force * Time.deltaTime)
      }
      

      匆匆写下,看看再写自己的:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多