I have this code:
var terrain : Terrain;
function FixedUpdate()
{
if (Input.GetButton ("Crouch"))
{
if (transform.position.y > terrain.terrainData.GetHeight(transform.position.x, transform.position.y))
{
transform.position.y =transform.position.y - transform.position.y/50;
}
}
}
And I'm getting this error:
"NullReferenceException: Object reference not set to an instance of an object Commander.FixedUpdate ()"
the error comes up when I press crouch and points to the 2nd if statement line.
can anyone help me resolve this error?
Thanks Much! I just used your code, but there is only one problem - for some reason it always returns a value around 65 and I have a very uneven terrain, so I don't know what I'm doing wrong but I've found a way working around this whole piece of code so i guess it doesn't matter. thanks though, I'll use this later I'm sure.
– anon322239Opps! Unity uses x and Z for the ground. You used x and y in GetHeight (which would be correct for 3DSMax, blender, etc... .)
– Owen-Reynolds