- Home /
Rigidbody2D's velocity Seems Erratic
I am making a 2D game where the player lands a spaceship on different planets. The way I calculate if there is a successful landing depends on the ships velocity and a few other variables. Everything seems to work fine except calculating the velocity. The problem is that the ships velocity will be a large number when the ship is at an angle other than 0 degrees on the z axis. Basically if the ship lands at any large angle the ship's velocity will be a large number compared to not landing at an angle. This should not happen.
In my code I multiply the ship's velocity's magnitude by 100 to make the number a bit more usable. When I print this number out while debugging I would get around .2ish when the ship lands at a 0 degree angle, and anywhere from 2-200 when landing in the same conditions, but at an angle other than 0.
I have the print statement run when the ship collides with the ground.
Here is the code. (Sorry if the the large if statement is hard to read)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipLanding : MonoBehaviour
{
public float maxLandingVelocity;
public float minRotateTolerance;
public float maxRotateTolerance;
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
print(gameObject.GetComponent<Rigidbody2D>().velocity.magnitude * 100);
// if statement checks for velocity of the ship, the orientation of the ship, and if both "legs" of the ship are contacting the ground
if (gameObject.GetComponent<Rigidbody2D>().velocity.magnitude * 100 < maxLandingVelocity && transform.rotation.z > minRotateTolerance && transform.rotation.z < maxRotateTolerance && RightLegCollider.rightLegTouching == true && LeftLegCollider.leftLegTouching == true)
{
SuccessLand();
}
else
{
print("Landing Failed");
}
}
}
void SuccessLand()
{
print("Landing Successful");
}
}
I should also mention the ship has a PolygonCollider2D component. I do not think this matters as I also tried it with a BoxCollider2D and got the same results.
Your answer
Follow this Question
Related Questions
Moving colliders that are part of a composite collider 1 Answer
How to Maintain Constant Velocity of a Moving GameObject? 0 Answers
Character slows down when running against a wall (2D) 1 Answer
Velocity doesnt change properly 1 Answer
Transform.position.x jumps to high numbers after collision 1 Answer