Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How can I get a GameObject's transform in game and set that to a variable?

$
0
0
I am trying to make a simple game where a cube follows you and you kill it. I want to instantiate the cubes, but my problem is setting the transform that the cube goes for After instantiating the enemy. When I don't instantiate and I just click play, it works (because the transform I use here is the GameObject of the first person controller, not the PREFAB). So my big question is, how can I get the transform of my first person controller and set that to the enemy's target? here's my enemy script: #pragma strict var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var myTransform : Transform; function Awake(){ myTransform = transform; } function Start(){ target = GameObject.FindWithTag("First Person Controller").transform; Debug.Log (target); } function Update () { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } Thanks for anyone who can help answer my question. If you need any more information I can provide it. PS I'm not good with Java OR C# but I prefer C#. I didn't do this code above, but everything else was all mine. Thanks for help.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>