The gameobject should rotate only around y axis. PlayerDistance should not be changed. i need this for another script.
The gameobjects should look to the player. (when the playerdistance < 10)
(my english is bad sorry).
using UnityEngine;
using System.Collections;
public class NM2 : MonoBehaviour {
public Transform player;
public float playerDistance;
public float rotationDamping;
public float x = 10f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
playerDistance = Vector3.Distance(player.position, transform.position);
if (playerDistance < x)
{
lookAtPlayer();
}
}
void lookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
}