- Home /
Rotate a character controller in the direction of input axis.
Im using the character controller.move example http://docs.unity3d.com/ScriptReference/CharacterController.Move.html
And im trying to rotate the player to the direction the character is moving with:
transform.LookAt(transform.position + movementVector);
However, this just makes the player rotate whenever any input is given on the horizontal axis, this causes the player to just keep turning whenever it moves sideways.
Preset directions wont work for me because im using joystick input, So diagonal directions have to work too.
Answer by $$anonymous$$ · May 15, 2015 at 07:09 PM
I've tryied something out, see if this is what you're looking for: (It ignores Y axis as movement)
private CharacterController CharCtrl;
private void Start()
{
CharCtrl = this.GetComponent<CharacterController>();
}
private void Update()
{
Vector3 NextDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (NextDir != Vector3.zero)
transform.rotation = Quaternion.LookRotation(NextDir);
CharCtrl.Move(NextDir / 8);
}
This works when directional input is being received but not when there is no directional input. How can we make Unity keep our character facing the direction of the previous input?
Good! But When Spawning Character Player $$anonymous$$oveing Up to the Surface(Ground) this is Not Well to Cover Problem
Answer by rjbresett · May 11, 2017 at 02:05 PM
Know this is an old thread but I was having a similar issue. While the character would rotate properly, it was only moving to the right. Note that I am using the reference script from the CharacterColtroller.Move API
The fix ended up being to multiply the move speed by -1 (if it is a positive number) if you were facing left and want to move left. You also need to make sure that it is returned to a positive number once you want to rotate and move to the right.
Answer by mayankela21 · Jun 07, 2020 at 10:42 AM
I find A Solution And Donot Ignore y Axis And Gontain Gravity, Working when Spawning thats Work:
public Joystick joystek;
public float movement_Speed;
CharacterController charCtrl;
public static float xMove, zMove;
//private Vector3 facDir;
void Awake()
{
charCtrl = GetComponent<CharacterController>();
}
void Update()
{
playerMovmentControler();
playerRotationControler();
}
// Update is called once per frame
void playerMovmentControler()
{
xMove = Input.GetAxis("Horizontal");// + joystek.Horizontal;
zMove = Input.GetAxis("Vertical");// + joystek.Vertical;
float gravity = 9.8f;
Vector3 moveAxis = new Vector3(xMove, -gravity, zMove);
charCtrl.Move(((moveAxis) * movement_Speed * Time.deltaTime));
}
void playerRotationControler()
{
if (xMove > 0)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 2f * Time.deltaTime);
else if (xMove < 0)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 2f * Time.deltaTime);
if (zMove > 0)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 2f * Time.deltaTime);
else if (zMove < 0)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 2f * Time.deltaTime);
}
When you Spawning Charactercontroller upper to the Surface Use this to proper rotation with Gravity