Hi, here is my problem.
When i rotate the camera and try to go forward, the character movement don’t care about the camera position and his forward is not the same as the camera’s forward.
for example if I move forward, then turn 90° around the character with the camera and try to move forward again, the player goes to the left not forward according to the camera’s position.
the camera rotation script
public class CameroFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float rotateSpeed;
// Start is called before the first frame update
void Start()
{
transform.position = target.position + offset;
}
// Update is called once per frame
void Update()
{
// if there is no target
if (target == null) return;
offset = Quaternion.AngleAxis(Input.GetAxis("Fire1") * rotateSpeed, Vector3.up) * offset;
transform.position = target.position + offset;
transform.LookAt(target.position);
}
}
the player movement script
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Rigidbody rig;
// Awake is called before game starts
void Awake() {
//get the Rigidbody component
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update() {
Move();
}
void Move() {
float xInput = Input.GetAxis("Horizontal");
float zInput = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
dir.y = rig.velocity.y;
rig.velocity = dir;
//rotating character
Vector3 facingDir = new Vector3(xInput, 0, zInput);
if (facingDir.magnitude > 0) { transform.forward = facingDir; }
}
}
I hope i explain my issue well, english is not my native language.
Thanks for your help.