- Home /
Transform.position values changing while object appears to be still?
Hi,
I have a gun object that is attached to an enemy character as a child of the hand object. There is an empty child object located at the end of the barrel that I am trying to use to spawn the bullets. For some reason the Y value of the transform.position for the spawning location is changing randomly, even though in the scene inspector it appears to be staying in the exact same position. I am getting the Y values outputted to the console with this line of code in a script attached to the empty object:
Debug.Log(this.transform.position);
When I move the gun object so that it is a child of the parent prefab (rather than the hand) and just sticking out of the enemy's chest then this issue doesn't seem to be a problem and the values outputted to the console match the apparent position of the object. Does anyone have any guidance on how to go about solving this issue?
I have attached a screenshot from the editor showing how things are set up if that is any help.
Is that the only place you're logging a Vector3?
It always puzzles me when people simply log a value. It makes it difficult to be sure what you're looking at in the console, unless you look at the call stack for each line.
So I would start by changing that line to (something along the lines of)
Debug.Log("$$anonymous$$uzzle position: "+transform.position);
@Bonfire-Boy Yes, I am logging the vector3 only for the muzzle object, I am logging it twice, once from the script attached to the muzzle itself, and once from the script attached to the gun that is actually responsible for spawning the ammo. Both values are always matching.
That's a bit confusing. You say "yes" and then expand it into a "no"!
Since you're logging the same information in more than one place, it would make sense (for your own sake as well as to help show the issue to other people) if the logging each time told us which place in the code it is.
By the way, you should only use the "Your Answer" box when providing an answer to your original question. I have converted to a comment for you so that your reply to my question appears under my question.
As for your original question, I'm not sure that we have enough information. You probably need to show us the code.
One possibility is that the transform.position gets set back to the correct value before it renders. Another is that the difference between 2.1 and -1.5 is too small for you to notice at the scale you're working at. Are you just relying on the Debug.Log or are you also inspecting the transform's position in the inspector?
And what do you mean by "it is changing randomly"? The screenshot shows it only changing from one value to another values. Do you just mean that you can't explain why it changes? Or that all sorts of different numbers are appearing in the logged vector? Or what..?
Answer by Panhypersebastos · Oct 17, 2016 at 05:00 AM
I figured out what was wrong. For the benefit of anyone in the future who stumbles across this thread, the problem was that the character had rigidbodies on its limbs. Once the rigidbodies were removed I stopped getting this problem.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Moving an object object x pos + speed?? 0 Answers
Set the position of a cloth 0 Answers
GUI button adjustments 2 Answers
Freeze rigidbody position without affecting rotation - please help! 1 Answer