- Home /
Detecting when the player has completed a full 360 degree spin
I've got a simple top-down mouselook setup, and I want to detect when the player has done a full spin. My current implementation is this:
public class PlayerController : MonoBehaviour
{
public float speed;
public float currentPlayerRotation;
public float lastFramePlayerRotation;
public string rotationStatus;
public float numOfSpinsClockwise;
// Start is called before the first frame update
void Start()
{
transform.position = Vector3.zero;
lastFramePlayerRotation = transform.rotation.eulerAngles.y;
}
// Update is called once per frame
void Update()
{
//WASD for movement
Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Rigidbody ourRigidbody = GetComponent<Rigidbody>();
ourRigidbody.velocity = inputVector * speed;
//Mouselook
Ray rayFromCameraToCursor = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane playerPlane = new Plane(Vector3.up, transform.position);
float distanceFromCamera;
playerPlane.Raycast(rayFromCameraToCursor, out distanceFromCamera);
Vector3 cursorPosition = rayFromCameraToCursor.GetPoint(distanceFromCamera);
transform.LookAt(cursorPosition);
//Movement and Mouselook code taken from Tom Francis
//Spincheck
currentPlayerRotation = transform.rotation.eulerAngles.y;
currentPlayerRotation = Mathf.Round(currentPlayerRotation);
if (currentPlayerRotation > lastFramePlayerRotation)
{
rotationStatus = "Clockwise";
} else if (currentPlayerRotation < lastFramePlayerRotation)
{
rotationStatus = "Anticlockwise";
} else
{
rotationStatus = "Stationary";
}
if (lastFramePlayerRotation == 359 && currentPlayerRotation == 0)
{
numOfSpinsClockwise += 1;
}
lastFramePlayerRotation = currentPlayerRotation;
}
}
I'm running into the issue where rotating the mouse in a clockwise direction rarely results in numOfSpinsClockwise increasing. From viewing the inspector the rotation variables don't appear to update fast enough to allow it to work, but I know this isn't the case. Does anyone have a fix/solution to this?
(I'm also very new to Unity so I apologise for any messy code/variable names).
Your answer
Follow this Question
Related Questions
How do I instantly change the direction of rotation of a gameobject? 1 Answer
On Collision my Player Spins Wildly 2 Answers
Set direction of rotation C# 1 Answer
Rotating projectile 2 Answers
Spin a 2D object with mouse drag 2 Answers