I can’t figure out a problem I have. It goes like this:
Let’s say I have an object (2d for simplicity’s sake) at (0, 0) and i want to move the object at an angle a distance then set an objects x and y to that. I thought it when something like: x/y = distance * sin/cos(angle). But this dose not seem to work for me. I’ve tried swapping sin and cos, but that doesn’t work. All the player dose is spin rapidly with little to no control. How do I fix this problem? Is sin/cos even the right thing to do?
Camera always looks at the player. Here is my script:
public class PlayerMovementScr : MonoBehaviour
{
PlayerInput input;
Vector3 move;
float moveAngle;
float moveAngleRelative; //Move angle RELATIVE to the camera
public GameObject playerCamera;
public float moveSpeed = 1.0f;
void Awake()
{
input = new PlayerInput();
input.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
input.Gameplay.Move.canceled += ctx => move = Vector2.zero;
}
void Update()
{
moveAngle = Mathf.Atan2(move.x, move.y) * Mathf.Rad2Deg;
moveAngleRelative = moveAngle + playerCamera.transform.eulerAngles.y;
transform.eulerAngles = new Vector3(0, moveAngleRelative, 0);
Vector3 m = new Vector3(Mathf.Cos(moveAngleRelative), 0, Mathf.Sin(moveAngleRelative)) * moveSpeed * Time.deltaTime;
transform.Translate(m, Space.World);
}
void OnEnable()
{
input.Gameplay.Enable();
}
void OnDisable()
{
input.Gameplay.Disable();
}
}