How do I change the player pivot?

I think I’m asking the right question.

I created a script to rotate the player with the left and right arrow keys, but it doesn’t rotate just the player, it also moves its position. I believe because the player is rotating around a pivot that’s not centered in the player.

Rotate Script:

using UnityEngine;

public class PlayerLook : MonoBehaviour
{
    public float rotationSpeed = .2f;

    public void ProcessLook()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(0, -rotationSpeed, 0);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(0, rotationSpeed, 0);
        }
    }
}

I have the camera set as a child of player. It kind of looks like the player is rotating around the camera. Help is greatly appreciated. Thanks.

This will rotate whatever game object this is attached to on its y axis. Find the game object with this component, set your tool handle to pivot position, and see where that is.

Unity doesn’t have a way to change the pivot of already imported assets. You need to ensure assets you import have their pivot in the correct place.

Or … just parent the mis-centered object to a blank GameObject… for the cost of an extra handful of multiplies every frame it gives you an awful lot of flexibility.

ALSO: beware of the Z and X keys to toggle pivot / local and center / global or whatever it is in the scene window… that can be very confusticating when you are authoring and aligning things.

This is a capsule with no parent. Wouldn’t the y-axis run straight down through the middle? Rotating around the y-axis I would think just spins the capsule in the same position.

I’m trying to create a character control script (left and right rotates the player, up and down moves the player forward and backward). I don’t want the player to be looking around and fall off a cliff.

Well, did you do what I suggested? What game object has this component, and what is it’s pivot? C’mon mate you need to investigate these things.

The player GameObject has the component attached.

Besides, I think it was my own fault. When modifying the code, I had the code using the arrow keys in one script to move left and right and another script was using the same keys to rotate.

Now I’m just having a heck of a time trying to get it to work at all now.

if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += transform.forward * forwardSpeed;
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position -= transform.forward * backwardSpeed;       
        }

This doesn’t work at all.

if (Input.GetKey(KeyCode.UpArrow))
        {
            _rigidbody.velocity = transform.forward * forwardSpeed;
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            _rigidbody.velocity = -transform.forward * backwardSpeed;       
        }

And this only works on one key press. Press up, it moves forward one, but no more. Press down, it moves back one, but no more.

private float verticalInput;

void Start() {
verticalInput = Input.GetAxis("Vertical");
}

void FixedUpdate() {
if (verticalInput > 0)
        {
            transform.position += forwardSpeed * Time.deltaTime * transform.up;
        }

        if (verticalInput < 0)
        {
            transform.position -= backwardSpeed * Time.deltaTime * transform.up;          
        }
}

This doesn’t work either.

This Works:

public void ProcessMove(Vector2 input)
    {
        Vector3 moveDirection = Vector3.zero;
        moveDirection.x = input.x;
        moveDirection.z = input.y;
        controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
    }

But, I don’t want to move the player with left and right keys. Only up and down arrows.

Well then get to debugging. Check if the code is being called as expected. Check if anything else is assigning to the velocity of the rigidbody, etc etc.

hmm. I did add velocity for gravity. Could that affect how velocity will work in forward and backward movement?

I’ve taken many courses, including the Junior Programmer Pathway, but I haven’t used velocity very much, so not sure “exactly” how it works.

Now, I may be looking at this the wrong way.

I’m creating a First Person Shooter and I was going to use left and right arrows for rotation and up/down arrows for movement. But, the course I’m in now is having me use the mouse for rotation. I personally don’t like using mouse for rotation because it makes it hard to click on anything (especially UI elements) without the rotation freaking out. But, for this type of game, is mouse for rotation better based on the controls of a FPS? I don’t play many FPS on a computer, but I’m guessing you use a mouse click to fire, plus mouse delta/mouse position to aim?

You don’t need a course to tell you this. You just need to read the documentation: https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

It’s just the current velocity of the rigidbody. If you assign to it, that becomes the current velocity. It can easily be reasoned that if you assign to it multiple times within a FixedUpdate call, then only the last one will do anything as you’ve overridden all the previous assignments.

If you want multiple sources for velocity, then you just need to aggregate them all (add them up all) beforehand and assign to the rigidbody’s velocity just once.

Also based on your code above that you edited in after my post, are you using both a rigidbody and a character controller? The both of them don’t work together. It’s either one or the other.

It’s 2023. We use the mouse to aim, unless you’re specifically making something hark back to the days of early Doom and Duke Nukem. If you want the player to aim with the mouse, but also have UI elements, then just turn off the mouse aiming while a UI element is open.

Well, I started on the right track then, according to the documentation. I did use velocity for my jump (as mentioned). But then, it might have gave me the clue I need. I should use AddForce for movement?

Actually, I wasn’t using Rigidbody with character controller. That was just part of a test/debug. I understand that Rigidbody conflicts with Character Controller.

That’s not what I’m saying at all. Seriously, read all my posts again.

You said this:

And I told you that multiple assignments to a rigidbody’s velocity means only the last assignment does anything.

Have you figured it out yet?

I’ve got my old way working where I use the mouse for rotation, which may be more suitable for this kind of game anyways. I bet if I rewrite my code, I’ll realize my mistake and be able to correct it. I’ll get to that a little later, if I choose to use keys for rotation.

Btw, I know what you were saying about velocity. I mentioned, AddForce because that’s what the documentation was referring to. It also said velocity is useful for jump because you want to apply immediate gravity.

On one of my above samples of what I’ve tried, it was almost “working”. I’ve determined that I need to use GetKey instead of GetKeyDown. GetKeyDown was only detecting one key press. So, if you hold the key in with GetKeyDown, it just executes one time. I changed it to this and it works as I was aiming for:

if(Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(-Vector3.up, rotateSpeed * Time.deltaTime);
        }

    if(Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);
        }