I am making a Player Detection System in c#. When the scene starts the game will instantiate the entity at a random point on the map. The problem is that for the Player Detection System to work, it needs to know the player’s Transform coordinates to detect how far away the player is. The Transform is stored in a variable that looks like this:
public Transform player;
The issue is every time I instantiate the entity (The entity with the script on it), it resets the value that I set the variable to, in this case, the Player.
The rest of the script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CotruxMovement : MonoBehaviour {
// Animation Control Variable
public Animator walk;
// Wandering Control Variables
public float moveSpeed = 3f;
public float rotSpeed = 100f;
public float chargeSpeed = 12f;
private bool isWandering = false;
private bool isRotatingLeft = false;
private bool isRotatingRight = false;
private bool isWalking = false;
// Attacking Control Variables
public Transform player;
public float attackBack;
public bool hostile = false;
private bool attacking = false;
// Knockback Variable
Rigidbody rigid;
void Start () {
rigid = GetComponent<Rigidbody>();
walk = GetComponent<Animator>();
}
void Update () {
// Wandering Controls Part 1
// ---
if (isWandering == false & !attacking)
{
StartCoroutine(Wander());
}
if(isRotatingRight == true & !attacking)
{
// Rotoate Right
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
walk.Play("CotruxIdle");
}
if (isRotatingLeft == true & !attacking)
{
// Rotate Left
transform.Rotate(-transform.up * Time.deltaTime * -rotSpeed);
walk.Play("CotruxIdle");
}
if(isWalking == true & !attacking)
{
// Moving Fowards
transform.position += transform.forward * moveSpeed * Time.deltaTime;
walk.Play("CotruxWalk");
}
// ---
// Player Detection & Attack Rotation
// ---
if (hostile == true)
{
if(Vector3.Distance(player.position, this.transform.position) < 33)
{
attacking = true;
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.5f);
transform.position += transform.forward * chargeSpeed * Time.deltaTime;
walk.Play("CotruxCharge");
} else {
attacking = false;
}
}
}
// Wander Timing
IEnumerator Wander()
{
int rotTime = Random.Range(1, 3);
int rotateWait = Random.Range(1, 3);
int rotateLorR = Random.Range(0, 3);
int walkWait = Random.Range(1, 3);
int walkTime = Random.Range(1, 5);
isWandering = true;
yield return new WaitForSeconds(walkWait);
isWalking = true;
yield return new WaitForSeconds(walkTime);
walk.Play("CotruxIdle");
isWalking = false;
yield return new WaitForSeconds(rotateWait);
if(rotateLorR == 1)
{
isRotatingRight = true;
yield return new WaitForSeconds(rotTime);
isRotatingRight = false;
}
if (rotateLorR == 2)
{
isRotatingLeft = true;
yield return new WaitForSeconds(rotTime);
isRotatingLeft = false;
}
isWandering = false;
}
// Player Collision Detection
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Player")
{
rigid.AddRelativeForce(-Vector3.forward * attackBack);
}
}
}
I also tried to set the variable to the Player by looking for the player’s tag:
balls = GameObject.FindGameObjectsWithTag ("Ball").transform;
But this did not work.