'Count Rotations in unity 2d game
I am making a game in which the character falls down on a slope and is constantly rotating clockwise at a fixed rotation. If it lands on it's feet it jumps.
The question is how to count a full spin(360) after touching the ground and how to multiply the jumping power for each complete spin, for ex. if the player manages to have a full spin and lands on its feet then the jumpower will be multiplied by X, if it manages two spins it will be multiplied by Y... and so on, but when it fails and touches the ground with other parts of the body I need to also reset the jumppower to initial value.
I tried using eulerAngles, but it doesn't work... Also I want to have a counter on screen that can show the user how many rotations has got after jumping and resetting when touching the ground, if its possible, I am new into unity and any help will be deeply appreciated.
This is the code that I used for counting rotations(also found on stackoverflow):
private void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
previousRollAngle = rigidBody.transform.eulerAngles.x;
}
private void FixedUpdate()
{
IsTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, GroundLayer);
if (IsTouchingGround)
{
rigidBody.velocity = transform.up * JumpPower;
}
//rigidBody.velocity = new Vector2(rigidBody.velocity.x, JumpPower);
// direction = rigidBody.transform.direction
// GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(0, JumpPower),ForceMode2D.Impulse);
//countspins
if (rollDegree >= 360)
{
rollDegree = rollDegree - 360;
leftSpins++;
}
else if (rollDegree <= -360)
{
rollDegree = rollDegree + 360;
rightSpins++;
}
rollDegree = rollDegree + (rigidBody.transform.eulerAngles.x - previousRollAngle);
previousRollAngle = rigidBody.transform.eulerAngles.x;
if (rightSpins == 1)
{
JumpPower = JumpPower * 3;
}
}
And this is the code that I used for rotating the player:
void Update()
{
GetComponent<Rigidbody2D>().angularVelocity = Spin;
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Rigidbody2D>().angularVelocity = CtrSpin;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|