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