- Home /
Object moves to 0,0,0? (Just want to know why it works)
pPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
foreach (GameObject s in eSpawn.eSnake)
{
if (bFireRate >= 0.75f)
{
Rigidbody bullet = Instantiate(eAi.Bullet, s.transform.position,
Quaternion.LookRotation((pPos - s.transform.position))) as Rigidbody;
bFireRate = 0.0f;
}
}
}
So the above script works.
Whats odd is that the below script doesn't:
pPos = eAi.Player.transform.position;
foreach (GameObject s in eSpawn.eSnake)
{
if (bFireRate >= 0.75f)
{
Rigidbody bullet = Instantiate(eAi.Bullet, s.transform.position,
Quaternion.LookRotation((pPos - s.transform.position))) as Rigidbody;
bFireRate = 0.0f;
}
}
}
Isn't it the same thing?
The eAi script is attached to a Main empty object in the world, which is then attached to a Empty object that is the child of the object that fires the bullet.
I use the child Empty object as a Sphere collier. Which contains the script that fires the bullet.
What does "doesn't work" mean? What behaviour were you expecting? What did you get? Where do you define "Player" in the eAi class?
Oh should have been a lot more specific.
Player is just a public Gameobject. Throughout the script I do call for the current position of the player, but I don't change it.
The above script sends the bullet object from the shooter to the player.
The below script sends the bullet from the shooter to the 0,0,0 of the world.
$$anonymous$$gestion to improve the code:
if (bFireRate >= 0.75f)
{
foreach (GameObject s in eSpawn.eSnake)
{
Rigidbody bullet = Instantiate(eAi.Bullet, s.transform.position,
Quaternion.LookRotation((pPos - s.transform.position))) as Rigidbody;
bFireRate = 0.0f;
break;
}
}
Answer by richyrich · Jan 18, 2015 at 03:20 AM
As far as I can tell, in the first script, you obtain pPos as world coordinates. In the script below, pPos is obtained from a child object where the location is 0,0,0 (i.e. zero offset to the parent's world coordinates). However, you are using the local offset as world coordinates, hence the position at 0,0,0.