- Home /
Getting error transform.position assign attempt for "Player" is not valid on rigidbody
I've seen others ask about this error and some say it appears in the editor only, some say it's because 1 or more of the Vector3 values is null.
I can see in the debug that my project thinks the x and z values are null - but I don't understand why, this is a rigidbody and I don't calculate the Vector3 through my code. I use AddRelativeForce() & AddRelativeTorque()
This error occurs when my player hits a wall in a specific part of a specific level. Could it be related to the bounce texture I have on my level mesh maybe? Honestly got no idea.
Edit: kmgr wanted to see code so here's the most relevant bit. Like I said, I don't edit the Player's transform directly. Only the rigidbody.
function FixedUpdate () {
var moveHorizontal : float= Input.GetAxis ("Horizontal");
var moveVertical : float= Input.GetAxis ("Vertical");
if((moveHorizontal < 0) || Input.GetKey("a"))
{
GetComponent.<Rigidbody>().AddRelativeTorque (0,-rotationSpeed,0);
} else if((moveHorizontal > 0) || Input.GetKey("d"))
{
GetComponent.<Rigidbody>().AddRelativeTorque (0,rotationSpeed,0);
}
if ((moveVertical > 0) || Input.GetKey("w"))
{
GetComponent.<Rigidbody>().AddRelativeForce (Vector3.forward * speed);
} else if ((moveVertical < 0) || Input.GetKey("s"))
{
GetComponent.<Rigidbody>().AddRelativeForce (Vector3.back * speed);
}
}
Actually no, I was wrong anyway since you use AddRelativeForce anyway (sorry I never used that). The Nan stuff https://en.wikipedia.org/wiki/NaN seems to happen most of the time by a division with 0. How do you set the speed and rotationSpeed?
Just to help with differential diagnosis, could you confirm whether the problem goes away or doesn't when those four AddRelativeTorque/Force lines are commented out?
Then I honestly don't know... it's weird. I read somewhere that having a transform.scale to 0 can cause that behaviour too but I guess it's not your case.
@barbe63:
NaN has several possible causes, however a division by zero usually results in (+/-)infinity. Only "0 / 0" would result in NaN like you can see in the wikipedia article you mentioned above. It's of course possible that a division by zero that results in infinity might cause a NaN further down the chain of calculations.
Since the mesh compression seem to have fixed it i guess the mesh collider simply has a too cmall triangle (area size -> zero) and during the collision calculations the forces become NaN at some point. $$anonymous$$esh compression will remove unnecessary geometry. So the error is most likely in the mesh / model.
Is this a case of Zero-area triangle? That would explain why compression solved it.
Answer by daviddickball · Jul 01, 2015 at 08:58 PM
I'VE SOLVED IT! I experimented disabling scripts, doing lots of weird things to figure this out. I noticed it always happened when my player collided with a certain place on the level mesh. So I played with mesh import options and changed the Mesh Compression from "off" to "Low"...
And it's now fixed. What an odd bug! Took me a long time to debug this too. Thanks everyone who commented!
Answer by FortisVenaliter · Jul 01, 2015 at 07:10 PM
So, the problem is likely in your speed or rotationSpeed variable. If either of them are NaN (Not a Number), then that will ruin the valid numbers in the rigidbody, causing the bug you are experiencing. If I were you, I'd go through the logic that computes those variables, especially divisions, to make sure they aren't dividing by zero or using imaginary math (X ^ -1, etc).
Short of that, you can simply sanitize your inputs by not calling those AddForce functions if Single.IsNaN() returns true.
Your answer
Follow this Question
Related Questions
Can a object without a rigidbody be triggered by a trigger? 1 Answer
custom rigidbody movement problem 2 Answers
Respawn player from Tornado Twin script issue 1 Answer
onmouseover javascript help 1 Answer
Assets/Standard Assets/Scripts/General Scripts/follow.js(9,59): BCE0044:unexpected char: 0xAD 1 Answer