- Home /
Quaternion.LookRotation Active when not active.
Can anyone help me understand why the Quaternion.LookAT code is being active when it supposed to not be. when the enemy Spawns its Target == null. only when a player with tag player walks into its trigger collider will allow the enemy to obtain a target and go into his state machine. Even before the enemy Collects the info on his target he look rigth at the player.
void Movement() {
if(Target != null) {
transform.localRotation = Quaternion.LookRotation(Target.transform.localPosition - transform.localPosition);
if(Vector3.Distance(Target.transform.localPosition, transform.localPosition) > meleeAttackDistance)
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
The players location shouldn't even be available at the time to run the function quaternion.lookrotation. But even before my player has time to enter the collider for my enemy to set that he is the target. The enemy spins around and looks right at the player. The player still hasn't even enter the collider in front of the enemy to check the player is indeed his target.
using UnityEngine;
using System.Collections;
public class AiController : MonoBehaviour {
enum State {Idle, Pursue};
State state;
public GameObject Target;
public float giveUpDistance = 20.0f;
public float meleeAttackDistance = 2.5f;
public float moveSpeed = 7.5f;
private IEnumerator AILogic()
{
while( state != State.Idle )
{
switch(state)
{
case State.Pursue:
Pursue();
break;
}
yield return null;
}
}
void Idle() {
}
void Pursue() {
if(Target != null) {
if(Vector3.Distance(Target.transform.localPosition, transform.localPosition) > meleeAttackDistance)
Movement();
if(Vector3.Distance(Target.transform.localPosition, transform.localPosition) > giveUpDistance) {
state = State.Idle;
Target = null;
}
}
}
void Movement() {
if(Target != null) {
transform.localRotation = Quaternion.LookRotation(Target.transform.localPosition - transform.localPosition);
if(Vector3.Distance(Target.transform.localPosition, transform.localPosition) > meleeAttackDistance)
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}
void Start() {
Target = null;
state = State.Idle;
}
void Update() {
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Player") {
if(Target == null) {
Target = GameObject.FindGameObjectWithTag("Player");
state = State.Pursue;
StartCoroutine( "AILogic" );
}
}
}
}
I only put a simple box collider in front of the enemy for this primitive player seen test. so when the player steps inside of the collider it grabs his tag and puts it as the enemy target. The movement code is within the state machine, yet even before it is active the Quaternion.LookRotation fires off. when player gets to like 50 distance from mob it turns and faces him.
What i am trying to do is get it so the enemy keeps looking the same direction, until the state machine gets activated and he follows the player. Enemy should only react if it sees the player, but at the moment he turns even if the player is not his target. I am not sure what i am missing, so that the enemy doesn't turn prematurely.
I don't see any bad logic in here. Put a Debug.Log() between lines 62 and 63:
Debug.Log(other.name + ", " + other.tag);
If this fires for 'Player', you know the issue is with how you've setup the collider(s) in your scene.
It does trigger, but the player never enters inside the box collider. $$anonymous$$y enemy is a sphere scale X:2 Y: 2 Z:2. was only mod to it. i added a box collider child it in the scene. reseted the values to the box collider.
Box collider Is Trigger checked material: none center x:0 Y:0.25 z:2.8 Size x:5 Y:0.25 Z:5
The player ticks it without entering. from 3 times the length of the collider. Even tho it ticks it never sets him as a target until the player actually enters inside the box collider. How can this the colider get triggered but not run the state code. the state is always idle unless player is actually inside the box collider.
According to you, something is triggering OnTriggerEnter().
In scene view, check the bounding boxes of each collider. $$anonymous$$aybe you have a vertex away from the main mesh in Character, or perhaps the collider is not centered on the player like you expect.
Check to see if any other objects in the scene are tagged 'Player' and my be triggering the code. You could a Destroy of whatever is triggering the collider and see what disappears:
Destroy(other.gameObject);
Thanks for the help robertbu, I am going to head another direction now. Not because it is hard, but i noticed having 2 colliders on an enemy has messed up my player targeting. Registering 2 enemies ins$$anonymous$$d of 1. $$anonymous$$aybe will look into raycast or a way to make a fan detection without a collider.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
AI enemy scripting help 1 Answer
How do I get my enemy to attack? 0 Answers
Can someone explain this part of the PlayerMoveController.js script in the AngryBots demo? 3 Answers