I watched a tutorial and followed it step for step, got everything working up until the Vector3.Angle( , ) part.
Whats suppose to happen is if the angle difference between contact.normal and vector3.up is less than 1 then I should be grounded, but it keeps saying I’m grounded when I jump onto walls even if they’re completely vertical.
So I checked Debug.Log to see what contact.normal says and what Vector3.up says and i get this for Vector3.up: (0,1,0)
and for contact.normal I get (x, y, z) numbers always ranging from -10 to 10.
Does anyone know why this isn’t working, and if not can anyone give insight to a different way to provide limits on angles that can be jumped from.
I removed a few variables and implemented numbers in their place, so that the code is easier to read.
```javascript
private var grounded : boolean;
function Update () {
if (Input.GetButtonDown("Jump") && grounded) {
rigidbody.AddForce (0, 25, 0);
}
Debug.Log (grounded);
}
function OnCollisionStay(collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
{ if (Vector3.Angle(contact.normal, Vector3.up) < 1); // having trouble with this line
grounded = true;
}
}
}
function OnCollisionExit ()
{
grounded = false;
}
```
You have a semicolon right after your condition in the if statement, so it looks like you’re always considered grounded even when you’re colliding into a surface that doesn’t have a relatively close normal angle to Vector3.up
if (Vector3.Angle(contact.normal, Vector3.up) < 1) //remove the semicolon
grounded = true;
Code is case-sensitive. When you try to set grounded to true, you used an uppercase ‘G’, which essentially refers to a completely different variable, so grounded remains false while Grounded (which is never accessed when you’re checking if you’re grounded) is true. Fix that to a lowercase ‘g’ and see if that gets you grounded again after the first jump.
The general lesson is that code is very picky. Never underestimate just how precise you have to be to make the code do what you want.