- Home /
lookat not updating my position(somebody out there has to know the answer)
This script is attached to my flyingEnemyPrefab. I have two cubes instantiating the flyingEnemy and this script makes the enemy fly to my location but when I move the enemies keep flying to my first location. The cubes are children of my player so they move along with me. If I walk to one side far enough and place the cube on top of my first location the enemies are literally flying straight down. I need for every new flying enemy that's instantiated to fly to my current location at the point of its instantiation.
var player : Transform;
var moveSpeed = 5;
var minDist = 0;
function Update ()
{
transform.LookAt(player);
if(Vector3.Distance(transform.position,player.position) > minDist)
{
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
I tested it and it works perfectly, what's it's your problem?
Oh that's a good notice, i did it so that's why i asked that xD
I read your question before, and did not understand the problem. If all these cubes are really children, then perhaps you want to change transform.localPosition...and move towards a local position of (0,0,0).
robertbu the cubes are children of my player so that they travel with him. They are invisible in fact I can replace them with an empty gameobject. This script is attached to the flying enemy that they instantiate. A flying enemy gets spawned once every 3 seconds but they always but even if I move the flying enemies continue flying to my first location.
Answer by robertbu · Jun 17, 2013 at 07:12 PM
I'm assuming you want the cubes to move towards the player/parent. You can use something like:
transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.zero, moveSpeed * Time.deltaTime);
no the cubes are fine where they are they are children of my player so they stay at the distance they are at. There's one on my left and one on my right. They both have a script that instantiates flying enemies. $$anonymous$$y problem is this script(above), this one isn't attached to the cubes it is ins$$anonymous$$d attached to my flying enemy prefab. The flying enemies keep flying to the location $$anonymous$$y player starts in but when I move my player they continue flying to the first location ins$$anonymous$$d of updating my location when I move.
Your code above is straight forward, and should work. Add a Debug.Log() statement in the Update() loop:
Debug.Log(player.position);
If this is not changing, then you've not initialized 'player' correctly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104241im_/https://answers.unity.com/themes/thub/images/avi.jpg)