Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I change the player pivot?

Discussion in 'Scripting' started by faulknordonald, Oct 3, 2023.

  1. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerLook : MonoBehaviour
    4. {
    5.     public float rotationSpeed = .2f;
    6.  
    7.     public void ProcessLook()
    8.     {
    9.         if (Input.GetKey(KeyCode.LeftArrow))
    10.         {
    11.             transform.Rotate(0, -rotationSpeed, 0);
    12.         }
    13.         else if (Input.GetKey(KeyCode.RightArrow))
    14.         {
    15.             transform.Rotate(0, rotationSpeed, 0);
    16.         }
    17.     }
    18. }
    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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
    arkano22 and Bunny83 like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    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.
     
    Bunny83 likes this.
  4. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.
     
  5. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
  7. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.UpArrow))
    2.         {
    3.             transform.position += transform.forward * forwardSpeed;
    4.         }
    5.  
    6.         if (Input.GetKey(KeyCode.DownArrow))
    7.         {
    8.             transform.position -= transform.forward * backwardSpeed;      
    9.         }
    10.  
    This doesn't work at all.

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.UpArrow))
    2.         {
    3.             _rigidbody.velocity = transform.forward * forwardSpeed;
    4.         }
    5.  
    6.         if (Input.GetKey(KeyCode.DownArrow))
    7.         {
    8.             _rigidbody.velocity = -transform.forward * backwardSpeed;      
    9.         }
    10.  
    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.

    Code (CSharp):
    1. private float verticalInput;
    2.  
    3. void Start() {
    4. verticalInput = Input.GetAxis("Vertical");
    5. }
    6.  
    7. void FixedUpdate() {
    8. if (verticalInput > 0)
    9.         {
    10.             transform.position += forwardSpeed * Time.deltaTime * transform.up;
    11.         }
    12.  
    13.         if (verticalInput < 0)
    14.         {
    15.             transform.position -= backwardSpeed * Time.deltaTime * transform.up;          
    16.         }
    17. }
    This doesn't work either.

    This Works:
    Code (CSharp):
    1. public void ProcessMove(Vector2 input)
    2.     {
    3.         Vector3 moveDirection = Vector3.zero;
    4.         moveDirection.x = input.x;
    5.         moveDirection.z = input.y;
    6.         controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
    7.     }
    But, I don't want to move the player with left and right keys. Only up and down arrows.
     
    Last edited: Oct 4, 2023
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
  9. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.
     
  10. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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?
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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.
     
  12. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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?
     
  14. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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.
     
  15. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    115
    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:

    Code (CSharp):
    1. if(Input.GetKey(KeyCode.LeftArrow))
    2.         {
    3.             transform.Rotate(-Vector3.up, rotateSpeed * Time.deltaTime);
    4.         }
    5.  
    6.     if(Input.GetKey(KeyCode.RightArrow))
    7.         {
    8.             transform.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);
    9.         }