- Home /
Smooth look at on Y axis only
Hi guys, so Im trying to make a basic enemy AI, and Im using a kind of cone of vision. For that, I have a literal cone, set as a trigger, and I want to make it look towards the player when the player enters it. So far I've tried a few solutions, but its always messed up and just sent the cone in a completely random rotation. Here is my code so far-
#pragma strict
var SeenPlayer : boolean = false;
var target : Transform;
function Start () {
}
function Update () {
if (SeenPlayer == true){
//This is where I want it to look at the player
}
}
function OnTriggerStay (other : Collider){
if (other.tag == "Player"){
SeenPlayer = true;
}
}
function OnTriggerExit (other : Collider){
if (other.tag == "Player"){
SeenPlayer = false;
}
}
In case it helps to know, by default my cone is 270, 315, 0.
Thanks guys
Answer by robertbu · Oct 30, 2014 at 04:21 PM
I'm assuming 'target' is a reference to the player:
var dir = target.position - transform.position;
dir.y = 0.0;
transform.Rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(dir), Time.time * speed);
'speed' is a variable you define and is measured in degrees per second.
Im having the same issue as before, it must be something to do with the model or something, because this keeps happening, with every solution

Quaternion.LookRotation() depends on the front side of your object facing positive 'z' when the rotation is (0,0,0). Your 'model' looks like a built-in cube, so that means your view cone needs to be attached so that the point is attached to the positive 'z' side of the cube.
Your answer
Follow this Question
Related Questions
Make an FPS gun point at the mouse position. 2 Answers
LookAt but using physics AddRelativeTorque 1 Answer
Lookat mouse, without raycasting? 1 Answer
How to send vars! 1 Answer
Autorotation to tween gameobject 0 Answers