I have this Commando-kind of top down shooter in the works. I have two game objects: player and one enemy. When player goes beyond < -30 along the Z-axis, enemy moves between z -25.0 - -20.0. After that it should stay put and just start rotating, where ever the player moves, but now enemy lunges back to -25.0 and starts sideways along the X-axis :? Here's the code: Any suggestions?
Do you want to make it so that the enemy starts walking towards you when he is close to the player and otherwise does some kind of patroling along that line?
No. He should move that distance (guy running to his post and then following the player) and nothing else. I've planned only having stationary or enemies, who move a little to keep it simple for now.
Code (csharp): var player = gameObject.Find("Player"); if (player.transform.position.z < -30) { var speed = Time.deltaTime * 5; transform.Translate(0, 0, speed); var enemy = GameObject.Find("Enemy"); transform.Translate(0, 0, speed); enemy.transform.position.z = Mathf.Clamp(enemy.transform.position.z, -25.0,-20.0); if (Mathf.Approximately(enemy.transform.position.z, -20)) { transform.LookAt(target); } YOu probalby just need to move all your transform.Translate calls int an else block after the if (Mathf.Approximately(enemy.transform.position.z, -20)) {
So I've edited this a couple of dozen times, asked some help and here's where I've come. However, Unity doesn't like, when I call a function when declaring a variable. How would I go tackling this? Code (csharp): var target : Transform; var speed = 5; function Update() { var player = gameObject.Find("Player"); if (player.transform.position.z < -30) { var enemy = gameObject.Find("Enemy"); transform.Translate(0, 0, speed); enemy.transform.position.z = Mathf.Clamp(enemy.transform.position.z, -25.0,-20.0); } else if (Mathf.Approximately(enemy.transform.position.z, -20)) { transform.LookAt(target); } } [/code]
I shouldn't answer if I'm not 100% certain this will work, but too bad for you, I will anyway Try changing var player = gameObject.Find("Player"); to just player = gameObject.Find("Player"); and adding this at the top: var player : GameObject; So you're not declaring it AND using the function at the same time. You could also use private var player : GameObject; if you didn't want the var cluttering up the Inspector window.