Hi, so while making my 2d side scroller script, I came across an issue in the code.
The character moves along the x axis, however, the part of my script which tells the character to face the right way, seems to only work in the z axis (so he always faces towards or away from the camera
if (moveDirection.sqrMagnitude>0.01)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection),1);
if (moveDirection.sqrMagnitude > 0.01) {
var angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
Note that the value of 1.0 as the last parameter in your Slerp() essentially causes that line to simply assign the rotation. If you want a Slerp():
if (moveDirection.sqrMagnitude > 0.01) {
var angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
}
Where ‘speed’ is a variable you define. Start ‘speed’ with a value of 5.0, and then adjust. You can also replace Slerp() with RotateTowards() and adjust ‘speed’ upward. This will result in a non-eased rotation.