- Home /
Rotate player in movement direction
Help please. Im trying to get my player to rotate to the direction he is supposed to go to. It works for W and S but A and D does not rotate, yet still moves. How to solve this? please tell me, i actually have no idea.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewPlayerController : MonoBehaviour {
public float speed;
public float jumpVelocity;
public bool doubleJumpAvaible;
public Animator anim;
public bool isGrounded;
public bool canMove;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
void FixedUpdate()
{
//Player movement
if (canMove)
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = new Quaternion(transform.rotation.x, 0, transform.rotation.z, transform.rotation.w);
transform.Translate(0, 0, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = new Quaternion(transform.rotation.x, -90, transform.rotation.z, transform.rotation.w);
transform.Translate(0, 0, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = new Quaternion(transform.rotation.x, 90, transform.rotation.z, transform.rotation.w);
transform.Translate(0, 0, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = new Quaternion(transform.rotation.x, -180, transform.rotation.z, transform.rotation.w);
transform.Translate(0, 0, speed * Time.deltaTime);
}
}
}
}
any help is appreciated.
Answer by Chimer0s · Feb 09, 2019 at 11:22 AM
If you're going to set the values manually like that, you may as well just use euler angles and save yourself the headache of using quaternions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewPlayerController : MonoBehaviour {
public float speed;
public float jumpVelocity;
public bool doubleJumpAvaible;
public Animator anim;
public bool isGrounded;
public bool canMove;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Player movement
if (canMove)
{
if (Input.GetKey(KeyCode.W))
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, transform.eulerAngles.z);
rb.velocity = speed * transform.forward);
}
if (Input.GetKey(KeyCode.A))
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, -90, transform.eulerAngles.z);
rb.velocity = speed * transform.forward);
}
if (Input.GetKey(KeyCode.D))
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, 90, transform.eulerAngles.z);
rb.velocity = speed * transform.forward);
}
if (Input.GetKey(KeyCode.S))
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z);
rb.velocity = speed * transform.forward);
}
}
}
}
I took the liberty of removing your update function as it shouldn't be there if it isn't doing anything. More importantly, I also switched your movement to be handled by giving your rigidbody velocity. You did well to handle the movement in FixedUpdate, but when moving a rigidbody you never want to move it by its transform. Doing so will prevent the physics engine from calculating proper collisions with it.
This should work well enough, but I'd suggest looking into lerping as a means of rotating the character to make the transition a bit more smooth. Good luck with your project!
Wow, thank you! also Wow, my questions dont get stuck forever in moderation anymore!
I have actually also gave up on the project(it's a 3D platformer) and i tried making a 2D platformer one, but i seem to get too lazy to work on anything. When i think about going back, i think about that making a 2D platformer would be amazing too and easier, but i wanna come back at the same time... any tips?
Also, the reason i did not use rigidbody movement was because i was afraid from my past experience that the player would feel terrible, unresponsive and uninteresting to control. Any tips on that?