Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player rotation relative to camera 180 Degrees

Discussion in 'Physics' started by GazingUp, Jan 15, 2020.

  1. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    I have an issue with the way my cube turns. I am trying to understand how the unit vectors work (transform.forward,right,and up)
    Here's my code:
    Code (CSharp):
    1.   void Update()
    2.     {
    3.    
    4.         _inputs = new Vector3(Input.GetAxis("Horizontal") * Speed, _body.velocity.y, Input.GetAxis("Vertical") * Speed);
    5.  
    6.         Debug.Log(transform.forward);
    7.         _body.velocity = _inputs;
    8.         if(_inputs != Vector3.zero)
    9.         {
    10.             transform.forward = new Vector3(_inputs.x, 0, _inputs.z);
    11.         }
    12.     }
    The start simply contains the rigidbody reference. Now the issue here is, when the player/character/cube thing is facing away from the camera, it moves away if I press W (or up), if I press A it moves left, D for right, and S towards the camera.
    The problem is when facing up and then I press down or press down and then press up or press left and then press right or press right and then press left, it does turn 180 degrees on the Y, however there's a strafe on the X axis of the transform.position, as if it did a snapping slow turn to the opposite direction.

    Can someone please explain why this is happening and how I can fix it? How can I make the character turn the opposite direction without the strafing ?I think I am missing something with transform.right ...
     
    Last edited: Jan 15, 2020
  2. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110
    Hi still a noob and still learning but peoples help here, so I try to helps to.

    1. if you have a rigid body never play directly with transform. it might caus bug.

    2. if your rigid body is kinematic = false don't play directly with velocity. the physic system don't like that.

    3. try instead using: addforce("what ever variable or input you lie", Forcemode."1 of the 4 forcemode")
    same thing with addtorque(to turn).

    personaly I prefere to use AddRelativeForce and AddRelativeTorque, but that just depend on how you use your camera.

    for point 3, an example of my script : rb.AddRelativeForce(Vector3.forward* forward, ForceMode.Acceleration);
    rb.AddRelativeTorque(Vector3.up * rotation, ForceMode.Acceleration);

    basicaly, AddForce (vector3.forward) tell unity: this object go "north"
    and AddRelativeForce (vector3.forward) tell unity this object move on IS foward axis.


    yup. I'm a noob so sorry if it's not clair. trying to help. :)
     
  3. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Alnos likes this.
  4. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Hey thank you for trying :) this is the new code here:

    Code (CSharp):
    1.  float h = Input.GetAxis("Horizontal");
    2.         float v = Input.GetAxis("Vertical");
    3.         Vector3 targetDir = cam.transform.forward * v;
    4.         targetDir += cam.transform.right * h;
    5.         targetDir.Normalize();
    6.         targetDir.y = 0;
    7.  
    8.         if(targetDir == Vector3.zero)
    9.         {
    10.             targetDir = transform.forward;
    11.         }
    12.  
    13.         Quaternion tr = Quaternion.LookRotation(targetDir);
    14.         Quaternion targetRot = Quaternion.Slerp(transform.rotation, tr, 10f * Time.deltaTime);
    15.  
    16.         transform.rotation = targetRot;
    17.  
    18.         body.velocity = new Vector3(h,body.velocity.y,v) * speed;
    I will take a look at addForce. Thank you! Im just used to velocity since that's what I used from my 2D Unity past.
     
  5. Alnos

    Alnos

    Joined:
    Aug 20, 2019
    Posts:
    110

    To me the seem like a good script.

    Problem I had with velocity, is when I jumps, I fall very slowly no matter the mass and the gravity.

    With AddFoce, you have 4 diferent way yo apply it and you have no more problems when jumping. :)
     
  6. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    For the jump slowness make sure you don't have a Time.deltaTime on the rigidbody.velocity.y, in a lot of movement tutorials (that don't use velocity) they put a 0 on the y axis. So when I had 0, the object fell slowly but if you just put the y velocity instead of 0, it should fall normally. You can even control the speed of fall by substituting a variable there too.

    But yeah, I'm curious about addForce.
     
    Last edited: Jan 16, 2020