- Home /
Calculate one lap completion Unity3d
Hii all..
I am having one circle sprite with circle collider. I am continuous rotating this circle on z axis by using :
transform.Rotate( 0 , 0 , speed * Time.DeltaTime);
Now i want to increment my score when circle completes rotation to 360 degree.
For that i have tried :
1) transform.rotation.z == 360
2) transform.Eulerangles.z == 360.
But this will not work as circle is continuous rotating with different speed.
So this condition will not satisfied. So what can be the another way to find complete rotation completion.
Thanks.
Answer by Veldars · Apr 11, 2015 at 06:02 AM
I think you can create some variable...
private float _prevRotation = 0;
private float _actRotation = 0;
void Start() {
_prevRotation = transform.rotation.z;
}
void Update {
_actRotation = transform.rotation.z;
if (_actRotation - _prevRotation >= 360) {
// Do what you want...
// Reset prevRotation
_prevRotation = transform.rotation.z;
}
transform.Rotate( 0 , 0 , speed * Time.DeltaTime);
}
I hope it's what you are looking for.
All right this code works for me (I've try it before sending it ^^)
using UnityEngine;
using System.Collections;
public class Rotate : $$anonymous$$onoBehaviour {
private bool _isPos = true;
private float _actRotation = 0;
private float speed = 50f;
void Update () {
_actRotation = transform.rotation.z;
Debug.Log(transform.rotation.z);
if ((_actRotation < 0 && _isPos) || (_actRotation > 0 && !_isPos)) {
// Do what you want...
Debug.Log("Complete");
// Reset prevRotation
_isPos = !_isPos;
}
transform.Rotate( 0 , 0 , speed * Time.deltaTime);
}
}
I hope this will work for you
Answer by hirenkacha · Apr 13, 2015 at 06:07 AM
@Veldars's code is also fine. If you want to check for EulerAngles then you can try this.
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour
{
private float currentRotation;
private float speed = 50f;
void Update ()
{
currentRotation = transform.rotation.eulerAngles.z;
if(currentRotation>360-speed * Time.deltaTime || currentRotation==360)
{
Debug.Log("===Complete");
}
transform.Rotate( 0 , 0 , speed * Time.deltaTime);
}
}