- Home /
How to check car has rotated a complete rotation i.e 360. so I can add 1 int value to score.. https://www.youtube.com/watch?v=9Ztd1XXmUGI like in this game Thanks.
How to check car has rotated a complete rotation i.e 360. so I can add 1 int value to score.. https://www.youtube.com/watch?v=9Ztd1XXmUGI
Vector3 strtAngle; Vector3 ChangedAngle; int ang;
float z_angle;
private void Start()
{
///////////////
strtAngle = new Vector3(0, 0, transform.rotation.eulerAngles.z);
ang = 0;
}
private void Update()
{
z_angle = transform.rotation.eulerAngles.z;
ChangedAngle = new Vector3(0, 0, transform.rotation.eulerAngles.z);
Debug.Log("finalAngle" +ChangedAngle);
float angle = Vector3.Angle(strtAngle, ChangedAngle);
Debug.Log("angle: " + angle);
// Vector3 targetDir = ChangedAngle - transform.chan;
// float angle = Vector3.Angle(targetDir, transform.forward);
if (angle > 350f)
print("Add score stuff.....!!!");
}
the value 360 is cant catch it. may be bcz of FPS if u check please help me.
OnCollisionEnter(Collision collisionInfo) & void OnCollisionExit(Collision collisionInfo) some times inAir not getting true after colliding ground so i used raycast to check ground.
void IsGrounded() { RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1.0f, lineLayer); // Debug.Log("Collided with:" + hit.collider.name); if (hit.collider != null && hit.collider.tag == "Ground") { isGrounded = true; // Debug.Log("GROUNDED"); } else { isGrounded = false; Debug.Log(" not GROUNDED"); } Debug.DrawRay(transform.position, Vector3.down, Color.green); }
Answer by Vincent1236 · Jul 04, 2018 at 10:41 AM
I think you are almost there. One thing, that will depend on how you want it, but I think you need to decide. Which is when the player can start spinning for points, which I think should be when leaving/not touching the track anymore (ie in air). I assume this is done by tagging ground as 'ground'
[SerializeField]
float previousAngle, currentAngle, flipAngle;
[SerializeField]
bool inAir;
[SerializeField]
int _360s = 0;
private void Update()
{
if (inAir)
{
currentAngle = transform.eulerAngles.z;
// Calculate the angle the bike has turned since the last frame and add to total rotation for this jump
flipAngle += Mathf.DeltaAngle(currentAngle, previousAngle);
previousAngle = currentAngle;
}
}
void OnCollisionExit(Collision collisionInfo)
{
Debug.Log("No longer in contact with " + collisionInfo.transform.name);
if (collisionInfo.gameObject.tag == "Ground") // you need to determine what counts as track/ground
{
inAir = true;
_360s = 0;
flipAngle = 0;
previousAngle = transform.eulerAngles.z;
}
}
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log("Now contact with " + collisionInfo.transform.name);
if (collisionInfo.gameObject.tag == "Ground") // you need to determine what counts as track/ground
{
inAir = false;
// You can decrease this ever so slightly, to accomodate for slopes. Or just add floats that account for take-off and landing-slopes
_360s = (int)flipAngle / 360;
if (_360s > 0)
Debug.Log("You did " + _360s + " front flips!!!");
else if (_360s < 0)
Debug.Log("You did " + _360s + " back flips!!!");
}
}
Answer by Janibasha · Jul 05, 2018 at 03:33 PM
Thanks for fast reply, I wrote the same but in the update function the condition not checking and more this function cannot catch the condition value
if (Mathf.Abs(strtAngle - currentAngle) > 360) { // no of rotations (count here) }
the value 360 is cant catch it. may be bcz of FPS if u check please help me.
OnCollisionEnter(Collision collisionInfo) & void OnCollisionExit(Collision collisionInfo) some times inAir not getting true after colliding ground so i used raycast to check ground.
void IsGrounded() { RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1.0f, lineLayer); // Debug.Log("Collided with:" + hit.collider.name); if (hit.collider != null && hit.collider.tag == "Ground") { isGrounded = true; // Debug.Log("GROUNDED"); } else { isGrounded = false; Debug.Log(" not GROUNDED"); } Debug.DrawRay(transform.position, Vector3.down,Color.green); } https://www.youtube.com/watch?v=9Ztd1XXmUGI
Will only be able to do some more work on this after the weekend as I am away. Yeah as long as you have a ground check works, whether it is raycast or collision based doesn't matter. In the mean time try using ins$$anonymous$$d of > 360 try > 300 or a smaller angle and see if the if passes.
If that works, just set srtAngle = currentAngle; inside the if. Otherwise you will get like thousands of logs/rotation counts
if do ins$$anonymous$$d of > 360 try > 300 or a smaller angle in update function the angle value goes to 300, 305,310,...350 . it skips different values in different times. may be bcz of frame rate..Please help .. i do try , THnks
Okay check my original answer. I have updated it. Good luck further! Oh and the idea will still work, no matter how you implement your grounding check as long as it works
not working...!!! getting rotation angle but not in one direction i.e when bike flips anti clock direction and not checking angle at <360 ... I checked last 2 days but can't find..plz help me. maybe u did not test the code.. ;-(( if u test I hope u definitely get the problem.
Your answer
