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

How do I make this equal to a Transform?

$
0
0
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BeeScript : MonoBehaviour { public float Speed; public Rigidbody rb; public Transform Target; // Start is called before the first frame update void Start() { rb = GetComponent(); // Get Rigidbody } // Update is called once per frame void Update() { FindClosestEnemy(); // Find Enemy with EnemyScript Roll();//Roll to Enemy } void FindClosestEnemy() { float distanceToClosestEnemy = Mathf.Infinity; Enemy closestEnemy = null; Enemy[] allEnemies = GameObject.FindObjectsOfType(); foreach (Enemy currentEnemy in allEnemies) { float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude; if (distanceToEnemy < distanceToClosestEnemy) { distanceToClosestEnemy = distanceToEnemy; closestEnemy = currentEnemy; Target = currentEnemy; // This Part wont work! ;-; } } Debug.DrawLine(this.transform.position, closestEnemy.transform.position); } void Roll() { rb.AddForce((Target.position - transform.position) * Speed); //Slower if closer } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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