Search Unity

Precisely rotate RigidBody of object to face direction of movement

Discussion in 'General Discussion' started by sachinhosmani, Mar 6, 2019.

  1. sachinhosmani

    sachinhosmani

    Joined:
    Mar 19, 2016
    Posts:
    4
    Hi,

    First off here is a demo of what I am trying to do : https://imgur.com/a/BExb2ho

    So I am trying to make the cuboid move such that mimics the movements of the mouse. If the mouse is dragged left, the cuboid should move left. Along with that, the face of the cuboid should also follow the direction of movement.

    My code is here:

    Code (CSharp):
    1. void Start () {
    2.    // Record where cuboid was initially
    3.    cuboidInitialPosition = cuboid.transform.position;
    4. }
    5.  
    6. void FixedUpdate () {
    7.    if (mousePressed) {
    8.        Vector3 diff = (Input.mousePosition - lastMousePos);
    9.        // y mouse movement is actually z in world space
    10.        diff.z = diff.y;
    11.  
    12.        // Some calibration. This works for me.
    13.        diff *= (3.6f / Screen.width);
    14.  
    15.        // If due to physics, the cuboid has flown up, put it back to its original y position
    16.        diff.y = cuboidInitialPosition.y - cuboid.transform.position.y;
    17.  
    18.        cuboid<Rigidbody> ().MovePosition (cuboid.transform.position + diff);
    19.        Vector3 velocity = diff / Time.deltaTime;
    20.        cuboid.GetComponent<Rigidbody> ().velocity = velocity;
    21.   }
    22.  
    23.    // Rotation part here
    24.    // Rotate only if there was any mouse movement...
    25.    if (new Vector3 (diff.x, 0.0f, diff.z).magnitude > 0.0f) {
    26.        // This was the movement vector from above, with the y movement set as 0
    27.        Vector3 angleUpdateDirectionDiff = new Vector3 (diff.x, 0.0f, diff.z);
    28.  
    29.        // Rotate to the target direction, but cap the rotation to a maximum
    30.        // The maximum is 360.0f * diff.magnitude which means allow a max speed of 360 degrees
    31.        // per unit distance traveled. I don’t want small mouse movements making drastic direction changes  
    32.        cuboid.GetComponent<Rigidbody> ().MoveRotation (Quaternion.RotateTowards (cuboid.transform.rotation, Quaternion.LookRotation (angleUpdateDirectionDiff), 360.0f * diff.magnitude));
    33.     }
    34. }
    My complaint is that the cuboid randomly oscillates in rotation and doesn't stay stable as visible in the video. What is wrong?

    Please note that this will be a physics game and so directly changing the object's transform will not work in my favour.

    Thank you
     
  2. sachinhosmani

    sachinhosmani

    Joined:
    Mar 19, 2016
    Posts:
    4
    I realise this is not the right place for this question. Can this thread be removed please?
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    You can use report button, to request removal, or simply to request moving topic to right sub forum.