Player rotation relative to camera 180 Degrees

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:

  void Update()
    {
    
        _inputs = new Vector3(Input.GetAxis("Horizontal") * Speed, _body.velocity.y, Input.GetAxis("Vertical") * Speed);

        Debug.Log(transform.forward);
        _body.velocity = _inputs;
        if(_inputs != Vector3.zero)
        {
            transform.forward = new Vector3(_inputs.x, 0, _inputs.z);
        }
    }

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 …
bm950

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. :slight_smile:

I got it!!! Wow so simple!Center pivot!!!

So make sure the mesh’s “local origin” is in the center of the model itself. This here is a cube model, so I just selected “Center pivot” and just put 0,0,0 on my collider’s positioning. Now it looks perfect! Look at this!e4dfi
I am so happy with this result. I learned something!

This is where I got the hint.

1 Like

Hey thank you for trying :slight_smile: this is the new code here:

 float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 targetDir = cam.transform.forward * v;
        targetDir += cam.transform.right * h;
        targetDir.Normalize();
        targetDir.y = 0;

        if(targetDir == Vector3.zero)
        {
            targetDir = transform.forward;
        }

        Quaternion tr = Quaternion.LookRotation(targetDir);
        Quaternion targetRot = Quaternion.Slerp(transform.rotation, tr, 10f * Time.deltaTime);

        transform.rotation = targetRot;

        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.

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. :slight_smile:

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.