Search Unity

Rotation is getting Hairy. Help!

Discussion in 'Scripting' started by renman3000, Jun 16, 2018.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi,
    I am using this code, to rotate my player, (camera matches rotation of player). It works fine. But after I move player, and try to rotate again, it gets very funky.

    Not sure the issue.

    Any thoughts?



    Code (csharp):
    1.  
    2.     void getAngle(Vector2 mousePos)
    3.     {
    4.  
    5.         Vector2 tap_pos = cam.ScreenToWorldPoint(mousePos);
    6.         float angle = Vector3.Angle(tf_player.up, tap_pos);
    7.  
    8.         ///angle, returns a value which is
    9.         ///0 -> 180 run from top to bottom on each side.
    10.         /// to insure one side is a negative... apply such if touch is to the left of player.
    11.  
    12.         if(mousePos.x < midScreen){
    13.             //mouse to left of player. angle is now negative.
    14.             angle = (angle *(-1)) + 360;
    15.         }
    16.  
    17.         applyRtn(angle);
    18.  
    19.     }
    20.  
    21.     void applyRtn(float angle){
    22.        
    23.         Quaternion quaternion = new Quaternion(transform.rotation.x, transform.rotation.y, angle, transform.rotation.w);
    24.         tf_player.rotation = Quaternion.Euler(0, 0, angle);
    25.         cameraFollow.transform.rotation = Quaternion.Euler(0, 0, angle);
    26.  
    27.     }
    28.  
    29.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I can't get your code to work. :)

    One problem is that line 5 will always return the camera position, not the touch position. This is because a zero Z value (i.e. a Vector2) is being handed into ScreenToWorldPoint (in that link, you will see they are using
    nearClipPlane
    for their Z value).

    Another thing I am not clear on is that there seem to be 2 cameras- "cam" and "cameraFollow". Or are they the same thing? You really only want one camera as you are trying to work out world points from the camera view.

    Also, is
    tf_player.up
    a Vector2 or Vector3? That could affect the outcome of the angle calculation if it can have a z value.
     
    renman3000 likes this.
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697




    Hi, I have swapped over to all physics based and have it working fine, on one condition: I set the Rigidbody Constraints to lock on x and z rotation axis. Otherwise, the ship does not rotate properly. Any ideas?


    Code (csharp):
    1.  
    2.      void spinLeft(float delta_mag){
    3.         float y_new = tf_player.localEulerAngles.z;
    4.         y_new = y_new - (additiveSpinValue + delta_mag);
    5.         Vector3 torque = new Vector3(0, y_new, 0);
    6.         rgby.AddRelativeTorque(torque, ForceMode.Force);
    7.         addDrag(y_new);
    8.  
    9.  
    10.     }
    11.     void spinRight(float delta_mag){
    12.         float y_new = tf_player.localEulerAngles.z;
    13.         y_new = y_new + (additiveSpinValue + delta_mag);
    14.         Vector3 torque = new Vector3(0, y_new, 0);
    15.         rgby.AddRelativeTorque(torque, ForceMode.Force);
    16.         addDrag(y_new);
    17.     }
    18.  
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I just created an empty scene, added a 3D cube object with a RigidBody (no constraints) and added your code into a script. Obviously I had to create dummy values for tf_player.localEulerAngles.z and additiveSpinValue. I also had to comment out AddDrag. But spinLeft worked fine (spinRight is 99% identical, you might want to consider coalescing them both into just a single function).

    So it seems to me that there are 2 possible reasons: first there could be something untoward in addDrag, or second, the ship may be taking forces from other objects / sources.

    What happens if you put your ship into an empty scene, does it work as expected then?
     
    renman3000 likes this.
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697

    hmmm.. I am actually using a hoverBody() function, which applies a force upward so the vehicle hovers. Removing it and the constraints, sees no tip, but I cant move forward....

    erm.

    Code (csharp):
    1.  
    2.  void hoverBody(){
    3.  
    4.         Ray ray = new Ray(tf_player.position, -transform.up);
    5.         RaycastHit hit;
    6.  
    7.         if(Physics.Raycast(ray, out hit, hoverHeight)){
    8.  
    9.             float proportionalHieght = (hoverHeight - hit.distance) / hoverHeight;
    10.             Vector3 appliedHoverForce = (Vector3.up * 10);
    11.             rgby.AddForce(appliedHoverForce, ForceMode.Acceleration);
    12.             ////print("appliedHoverForce = " + appliedHoverForce);
    13.  
    14.             if(hit.collider.CompareTag("onTrack")){
    15.  
    16.                 if(isOnTrack){
    17.                    
    18.                 }
    19.                 else{
    20.                     isOnTrack = true;
    21.                 }
    22.             }
    23.             if (hit.collider.CompareTag("offTrack"))
    24.             {
    25.                 if (!isOnTrack)
    26.                 {
    27.  
    28.                 }
    29.                 else
    30.                 {
    31.                     isOnTrack = false;
    32.                 }
    33.  
    34.             }
    35.         }
    36.  
    37.  
    38.  
    39.     }
    40.  
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I have just added the hoverBody to my 3D cube, still with no physics constraints and still it does not tip. Something else must be causing it. :)

    You could try using Physics Debug Visualization, or, creating a blank new scene with a 3D cube and keep adding things to it one-by-one to find out the thing that is causing the tipping.
     
    renman3000 likes this.
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Will do.
    Thanks

    Edit:
    @Doug_B
    So I just made a video, the issue is that when constraints are on, the system works fine. When off, I can only turn clockwise for some reason. If you have any ideas, please pass on.

    Cheers

     
    Last edited: Jun 22, 2018
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Clearly something differs between the two directions, so it becomes a case of finding what that difference is. :)

    While it is a little difficult to follow the video (it is not clear, for example, what control key is being pressed when), it almost looked like there was an extra call to "rotate right" occuring. So rotating right almost looked ok, whereas turning left did a "left-then-right" making the problem more obvious. But that is just a guess based off the video and could be wildly inaccurate.
     
    renman3000 likes this.
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697

    Hmmm.
    thanks
    !!!