- Home /
Rotate to face player, drunk issues
Hello, been Unity Scripting for a while but still trying stuff out, right now I am trying to make an object turn and move to the player.
My main issue is the object will turn to face the player's starting position, but not when I move the player through typical control as a player would be.
If I move the player through the editor it works fine, turning and moving. I have a feeling this is some issue with me using Pos.position and not a Vector3, see the code.
My other issue is when I move the player which is a typical FPS player controller, the object following it goes at wierd angles as if it is drunk.
Here is the code:
using UnityEngine;
using System.Collections;
public class RemoteMovement : MonoBehaviour {
public Transform PlayerPos;
public float strength = 1f;
float str;
Quaternion targetRotation;
// Use this for initialization
void Start () {
}//START
// Update is called once per frame
void LateUpdate () {
if( Input.GetKey("i") ){
transform.Translate( transform.forward*0.05f);
}//if
targetRotation = Quaternion.LookRotation (PlayerPos.position - transform.position);
str = Mathf.Min (strength * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
Debug.Log("Player Pos: X;"+PlayerPos.position.x+" Y;"+PlayerPos.position.y);
Debug.Log("targetRotation: "+targetRotation);
transform.Translate( Vector3.forward*0.05f);
}//UPDATE
}//CLASS
I remove the answer but you still need to change for tranform.forward, or your guy will only move in the same direction Vector3.forward.
Fair enough, but currently my only issue is rotating without going drunk, and rotating to a moved player or object through scripts, not if I move it through the editor.
There is a part about rotation there :http://unitygems.com/basic-ai-character/#Start
Ok I have been looking at this stuff, it makes a decent amount of sense but when I implement it, something doesn't work.
$$anonymous$$y following object turns, but never all the way to the player and still only when I move the player through the editor.
public Transform PlayerPos;
Vector3 newRotation = Quaternion.LookRotation(PlayerPos.position - transform.position).eulerAngles;
newRotation.x = 0;
newRotation.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime);