物体旋转的Unity3d问题

时间:2018-11-13 21:38:09

标签: unity3d

我有一个球在x轴上直线移动,并且可以在z轴上左右移动。到目前为止还不错,但是我根本不希望我的球向上运动,所以我使用了刚体中的约束并锁定了Y位置,但这带来了另一个问题,我的球不再旋转了。 我尝试在脚本中执行此操作,并且发生了相同的事情。 任何想法如何解决这个问题? 我编辑它。这是我的密码。我希望可以帮助

private Rigidbody rb;
[Tooltip("How fast the ball moves left/right")]
public float dodgeSpeed;

[Tooltip("How fast the ball moves forwards automatically")]
[Range(0, 10)]
public float rollSpeed = 5;

//Stores the starting position of mobile touch events
private Vector2 touchStart;




void Start ()
{
    // Get access to our Rigidbody component
    rb = GetComponent<Rigidbody>();
}

void Update()
{
    ray ();
}

void FixedUpdate ()
{       

    StartCoroutine (waitToStartMoving ());
}



void Movement()
{
    float horizontalSpeed = 0;

    /*// Check if we're moving to the side
    float horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;

    Vector3 movementForce = new Vector3(rollSpeed, 0.0f, horizontalSpeed);
    // Time.deltaTime is the amount of time since the // last frame (approx. 1/60seconds) 
    movementForce *= Time.deltaTime * 60;
    // Apply our auto-moving and movement forces 
    rb.AddForce(movementForce);
    //rb.AddForce(horizontalSpeed, 0, rollSpeed);*/


    // Check if Input has registered more than zero touches
    if (Input.touchCount > 0)
    {
        // Store the first touch detected.
        Touch touch = Input.touches[0];
        horizontalSpeed = CalculateMovement(touch.position);

    }


    // Apply our auto-moving and movement forces
    //rb.AddForce(horizontalSpeed, 0, rollSpeed);

    var movementForce = new Vector3(rollSpeed, 0, horizontalSpeed);
    // Time.deltaTime is the amount of time since the // last frame (approx. 1/60seconds) 
    movementForce *= (Time.deltaTime * 60);
    // Apply our auto-moving and movement forces 
    rb.AddForce(movementForce);
}

IEnumerator waitToStartMoving()
{
    yield return new WaitForSeconds (5f);
    Movement ();
}

// Will figure out where to move the player horizontally
// <param name="pixelPos">The position the player has touched/clicked on</param>
// <returns>The direction to move in the x axis</returns>
float CalculateMovement(Vector3 pixelPos)
{
    // Converts to a 0 to 1 scale
    var worldPos = Camera.main.ScreenToViewportPoint(pixelPos);
    float xMove = 0;
    // If we press the right side of the screen
    if (worldPos.x < 0.5f)
    {
        xMove = -1;
    }
    else
    {
        // Otherwise we're on the left
        xMove = 1;
    }
    // replace horizontalSpeed with our own value
    return xMove * dodgeSpeed;
}

void ray()
{

    Debug.DrawRay (transform.position, Vector3.down, Color.red);
    if (!Physics.Raycast(transform.position, Vector3.down, 5f))
    {
        rb.constraints = RigidbodyConstraints.None;
        //Camera.main.GetComponent<CameraBehaviour> ().gameOver = true;
        //GameObject.FindObjectOfType<Score>().stop = true;
        //StartCoroutine (WaitToRestart ());
    }
}

}

1 个答案:

答案 0 :(得分:0)

我的球对象在Y轴上高1个单位。因此,当我检查y约束位置时,我的球没有碰到地面,所以它可以旋转。将我的球降低到0.8的位置效果很好。 感谢您的答复。