0
\$\begingroup\$

I have a bullet that has to hit a constantly moving enemy.

So, in BulletScript, I have declared a Transform

public Transform enemy;               //and assigned enemy object to it that is continuously moving and changing its position

Now, when I try to use enemy.position in bullet script so as to hit it, enemy.positiongives the position at which the enemy started and not the position at which it was when bulletprefab was shot.

How can I get the updated position of enemy object every time bulletprefab is instantiated.

\$\endgroup\$
  • \$\begingroup\$ Your comment says the object is contantly changing its position, but the text after it suggests its position stays the same. Which is it? \$\endgroup\$ – Anko Jul 27 '14 at 12:40
  • \$\begingroup\$ @Anko: enemy object is constantly moving and I am trying to get its updated position in the bullet script, and so created Transform called Target and assigned enemy object to Target, but when I try to use Target.position, I get the position at which the enemy(Target) starts to move from and not the position at which it currently is \$\endgroup\$ – Simran kaur Jul 27 '14 at 12:44
1
\$\begingroup\$

How are you retrieving the value of enemy? It is assigned at compile time, or retrieved at runtime?

My advice would be to declare it as a GameObject, assign its value at runtime and then access its Transform value.

\$\endgroup\$
  • \$\begingroup\$ Could you please give an example: Say, instead of public Transform, I declared, Public GameObject , then? How do I assign the GameObject at runtime? \$\endgroup\$ – Simran kaur Jul 27 '14 at 13:30
  • \$\begingroup\$ I think, what you just mentioned is the problem. because, The position to it is being assigned at compile time and retrieved at runtime, but I am not clear that how do I change that, please? \$\endgroup\$ – Simran kaur Jul 27 '14 at 13:34
  • 1
    \$\begingroup\$ Well, the answer is: It depends on your game logic! How is the target assigned? If there is only one target and it will never change, you can assign a tag or name to that object at compile time and retrieve it at runtime. If there are several of them then you should define your game logic that decides which object is retrieved. Ultimately what you need to do depends on what you want to do! If you want a more concrete answer, please provide more details. \$\endgroup\$ – angarg12 Jul 28 '14 at 6:29
  • \$\begingroup\$ Yes, it's just one object translating and changing its position, I assigned the object in the Inspector Panel \$\endgroup\$ – Simran kaur Jul 29 '14 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.