- Home /
Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning.
Hello :)
I am currently developing a little 2D project of mine, and to put it in simple terms, I have an enemy that fires a gameObject when it is within a certain distance of the player. I have created the projectile as a trigger-able prefab and was having trouble figuring out how to make the object not follow the player, but move in the direction that the player was in upon it's spawning. (As a separate script)
In the below script, what would I have to replace 'left' with?
void Update () {
transform.Translate(Vector3.forward * Time.deltaTime);
transform.Translate(Vector3.left * Time.deltaTime, Space.World);
Answer by revolute · Aug 24, 2014 at 03:56 AM
Make a Vector3 variable in the class and get player position in Start() function. Use that to go to where the player was on spawn.
Sorry, but in game development, coding is my weakness.. I'm not asking you to do it for me but could you please clarify how one would accomplish this?
You can either make a public variable in your class and bind your player object from editor screen. Or you can do GameObject.Find("name of your player object").