Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

problem extracting transform from an array

$
0
0
hi all, this is a script to move an object in V3 towards a target object, the target object is randomly chosen from an array of object with a certain tag, the main issue is in my find target function but im lost, any help would be appreciated #pragma strict var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning //var tower: GameObject; var myTransform : Transform; //current transform data of this enemy var targets : GameObject[] ;//hold an array of target objects var target : Transform; //the enemy's target function Awake() { myTransform = transform; //cache transform data for easy access/preformance } function Start() {//find all the game objects tagged as target targets = GameObject.FindGameObjectsWithTag("target"); //if no target set run function find target if(target==null){ findTarget(); } } function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.transform.position - myTransform.position), rotationSpeed*Time.deltaTime); //move towards the player myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } function findTarget(){ //randomly pick a target from the collected array var newTarget : int = Random.Range( 0, targets.length ); //set the chosen randomly picked target as the curren target target = targets[newTarget].transform.position; }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



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