How do I make this equal to a Transform?
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<Rigidbody>(); // 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<Enemy>();
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
}
}
Comment
Best Answer
Answer by dan_wipf · Mar 21, 2019 at 06:28 PM
Target = currentEnemy.transform
i guess this is what you’re looking for. you’re trying to adapt a Target(transform) to currentEnemy(Enemy) those types do not match, so you have to grab the Transform from the Enemy Class where Enemy is attached to