Problem with instanced position [solved]

Hi,

I’m still working my way through the FPS tutorial, and I’ve run into a strange problem. Originally, when the enemy robots fired missiles, they would appear exactly in front of their guns, as expected, because I have placed the gun_spawn object there. It worked fine.

Now however, it doesn’t work anymore. The gun_spawn object is still at the right place - I can see that in the scene view, both during editing and while playing the game. But the rockets appear far below the gun_spawn location. Their X and Z coordinates are correct, but the Y coordinate is way off. Furthermore, the amount that the Y coordinate is off varies, and seems to increase for each rocket fired.

You can see the problem here:
http://runevision.com/multimedia/unity/fps.html
The first rockets fired by the enemies are way too low; the next appear below the ground, so they are eventually not visible.

I haven’t modified any of the scripts, so I have absolutely no idea what could be causing this behavior!

Also, the ragdolls appear at wrong positions too sometimes (I never got that to work correctly), which may or may not be the same problem.

Any idea what the problem is caused by?

Thanks in advance,
Rune

hmm - the rockets look fine here (like they’re coming out the barrel)

EDIT: ok now i see what you mean - so if the spawn point is set up correctly my guess would be something in another script is moving or resetting the y position.

I don’t understand it. I’m only using the scripts that the tutorial instructs to use, and none of those are changing just the Y positions…

Rune

i just opened the tut - looking around (i haven’t perused it in a while ; )

maybe try selecting the spawn point so its visible in the scene view, start the game to see if it changes position at any time (pause frame advance if need be) - if it doesn’t then at least you know its a most likely a problem with the projectile.

i’ll post if i spot anything.

I already did that, and the spawn point stays where it’s supposed to while the rockets appear far below it.

I tried modifying the Y value myself now, right after the rockets are created:

// create a new projectile, use the same position and rotation as the Launcher.
		var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
		instantiatedProjectile.position.y = 4.1; // Inserted by me

With this line, all the rockets are shot in the same height (4.1 as specified), so whatever is creating the problem, it is something that happens before this.

Rune

maybe someone else will figure it out but i don’t see anything unusual - it works fine in the full tut scene here - maybe it would help if you posted a screenshot of your inspector with the rocket the full script?

I figured out how to print out stuff, and the debug information I printed is very weird indeed. Basically, the position of the robot written in the console is not the same position shown in the inspector…

In the console, both the global and local position of the robot is printed as 3.6 - however, the inspector shows that the location is 3.93.

For each rocket fired, the y position of the robot reported by the console gets lower, but in the inspector it stays at 3.93 all the time.

[Edit: Note that I deleted the other robots so that there is only one in the scene to avoid confusion about console readout.]

Any idea what is going on?

Rune

Okay, I have a breakthrough, although I think it shows that the problem goes deeper than I had hoped.

I tried printing out the robot’s global position and velocity in each Update (1) and LateUpdate (2):

function Update ()
{
	print("1 robot global pos is  "+gameObject.transform.position+", vel is "+gameObject.rigidbody.velocity);
}

function LateUpdate ()
{
	print("2 robot global pos is  "+gameObject.transform.position+", vel is "+gameObject.rigidbody.velocity);
}

This is what I got:

1 robot global pos is  (-66.5, 3.8, 3.3), vel is (0.0, -4.9, 0.0)
2 robot global pos is  (-66.6, 3.9, 3.3), vel is (0.0, -4.9, 0.0)
1 robot global pos is  (-66.6, 3.8, 3.3), vel is (0.0, -5.1, 0.0)
2 robot global pos is  (-66.7, 3.9, 3.3), vel is (0.0, -5.1, 0.0)
1 robot global pos is  (-66.7, 3.8, 3.3), vel is (0.0, -5.3, 0.0)
2 robot global pos is  (-66.7, 3.9, 3.3), vel is (0.0, -5.3, 0.0)
(...)
1 robot global pos is  (-66.8, 3.5, 3.3), vel is (0.0, -20.2, 0.0)
2 robot global pos is  (-66.8, 3.9, 3.3), vel is (0.0, -20.2, 0.0)
1 robot global pos is  (-66.8, 3.5, 3.3), vel is (0.0, -20.4, 0.0)
2 robot global pos is  (-66.8, 3.9, 3.3), vel is (0.0, -20.4, 0.0)

As you can see, even though the robot stays on the ground all the time, the downwards velocity just keeps increasing. In each frame when this velocity is applied, the robot is moved downward by an ever increasing amount. After that the robot is moved back up on the surface due to collision detection, but the velocity is not corrected to reflect this adjustment!

If some script is reading the position of the robot, it gets completely different results depending on if it reads the position before or after the robot has been moved back up on the surface. I don’t know how this order can be controlled in unity, but in any case, there’s something seriously wrong going on with the velocity. When the robot is not falling down, the Y velocity ought to be 0 (or at least extremely small!)

What to do now…? Is it the user’s responsibility to fix the velocity, or should it be handled automatically by the physics in Unity?

Rune

I found the problem now. I had accidentally attached a RigidBody component to the robot, and it interferes with the CharacterController, which is an alternative to RigidBody. I had misread the tutorial and thought that the robot should have the DamageReceiver script to get damage, like all the other things that take damage. The DamageReceiver script requires the object to have a RigidBody, so… The robot should of course have the CharacterDamage script instead. That’ll teach me to read more carefully. But hey, I learned a lot making this mistake!

Thanks for looking into this and bearing over with me. :slight_smile:

Rune

Sorry to dig out this old topic but since the tutorial is renewed… :slight_smile:

I’ve encountered exact same issue but with player’s rocket launcher.
Indeed the rigidbody is the culprit here.
Solution is simple, just disable gravity on the rigidbody of the player.
I know this may be obvious to most of you, but in case someone else gets stuck with it like me I’m posting it here :slight_smile: