Controls and rotation arent linked

Help! when I turn 180 degrees in my game the controls don’t follow, so the controls would be flipped, “W” would be backward and “S” would be forward, etc. Any advice?

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float runSpeed = 5f;
public float speedH = 2f;
public float speedV = 2f;
private float yaw = 0.0f;
private float pitch = 0.0f;

// Start is called before the first frame update
void Start()
{
runSpeed = 5f;
}

// Update is called once per frame
void Update()
{
transform.Translate(runSpeed * Input.GetAxis(“Horizontal”) * Time.deltaTime, 0f, runSpeed * Input.GetAxis(“Vertical”) * Time.deltaTime, 0f);
yaw += speedH * Input.GetAxis(“Mouse X”);
pitch -= speedV * Input.GetAxis(“Mouse Y”);
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}

did u try
transform.Rotate(axis, rotationDegree,Space.Self);
for local rotation

i remember when trying to rotate i noticed that euler angles were not what i expected
because rotation is done using quaternions internally and for display purpose it is available as euler.

where do I put that line of code?

Could you rewrite the line of code that i need to change to make it work? or show me an example. I dont know where to put the code you told me to write.

transform.Rotate(pitch, yaw, 0.0f,Space.Self);
if pitch along x axis
yaw along y axis
0 along z axis

i put in transform.Rotate(pitch, yaw, 0.0f,Space.Self); replacing transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); so now the code is
void Update()
{
transform.Translate(runSpeed * Input.GetAxis(“Horizontal”) * Time.deltaTime, 0f, runSpeed * Input.GetAxis(“Vertical”) * Time.deltaTime, 0f);
yaw += speedH * Input.GetAxis(“Mouse X”);
pitch -= speedV * Input.GetAxis(“Mouse Y”);
transform.Rotate(pitch, yaw, 0.0f, Space.Self);
}
}

but when I play, the screen goes berserk! its starts rotating really fast. help!?

add a smoothing factor to ur input.getaxis

yaw += speedH * Input.GetAxis(“Mouse X”) * 0.01;
pitch -= speedV * Input.GetAxis(“Mouse Y”) * 0.01;

adjust smooth factor as required by trial

the rotation was fine but I need help with the movement still because the controls are still not linked. sorry for all my one corrections btw.

let me try ur code from my side.

What is the mapping of ur horizontal axis & vertical axis ?
post screenshot from Input

its moving fast becos the prev yaw & pitch values are being used to rotate again & again
if u don’t want that
then call rotate when u need it
the risk in using transform.eulerangles
4725704--447311--upload_2019-7-8_23-34-30.png

4725704--447308--upload_2019-7-8_23-32-41.png

What do you mean? What are you looking for?

4725725--447314--upload_2019-7-8_23-35-23.png

4725725--447317--upload_2019-7-8_23-35-50.png

My code is not that complex, it only has float values for the X and Y rotation and the movement speed.4725734--447320--Notalotgoingonhere.PNG

Which axis is Horizontal for u ? X / Y / Z
Which axis is Vertical for u ? X / Y / Z

“Z” = Vertical, “X” = Horizontal.

“when I turn 180 degrees in my game”
along which axis ?

It doesn’t matter. the controls just don’t follow the rotation, so speaking globally if you turned 90 on the Y the controls would be “W” for moving left and “S” would be moving right “A” would be backward and “D” would be forward.

Put your character (the thing you want to control) in an empty game object. Apply the movement on that but apply the rotation on the original. So you will have a GO which does not rotate and always moves in relation to the WASD keys (or what you set), but the visual elements and the inner character will rotate according to the desired rotation.

what do you mean by “Apply the movement”?