- Home /
I want to rotate the model to face player, not bend?
I posted this question about 7 hours ago but included the wrong code, for some reason the post won't edit so I'm posting again. I basically want my enemy model to always face the player but not bend when the player is close.
EDIT: Sorry, what I'm trying to get is I only want the model to rotate left/right, not up and down. The model will rotate so it's facing the sky if I get close, I just want the model to turn around, not bend backwards/forwards.
var PlayerHealth = 10; //PlayerHealth Value
var TakeDamage : boolean; //Damage toggle
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
//When the player and AI meet enable Damage Toggle
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
TakeDamage = true;
}
}
//When the player and AI seperate disable Damage Toggle
function OnTriggerExit(other : Collider){
if(other.tag == "Player"){
TakeDamage = false;
}
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //Target the player
}
function Update () {
//Rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//Move towards the player
//myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I'm guessing that must be some kind of camera perspective bending? It's pretty hard to bend anything in Unity without a plugin.
No? The script rotates the enemy model to face the players position, it's just that when the player gets close the model will bend backwards to remain facing the player's position.
Could you possible post a screen grab of the effect you are seeing? I would imagine the enemy is probably just getting too close to be realistic - effectively being "within" the player.
Here is what it's doing:
It "bends" backwards so to speak, I guess it's more of a rotation now though.
Yeah - that's comedy :) BTW not really bending - that's leaning - but I see your point!
Answer by whydoidoit · Oct 13, 2012 at 08:36 AM
Just rotate around the Y axis:
var newRotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime).eulerAngles;
newRotation.x = 0;
newRotation.z = 0;
myTransform.rotation = Quaternion.Euler(newRotation);