- Home /
How can the gameobject rotate only rotate around the y axis???
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);
}
}
Answer by mbraley · Dec 20, 2016 at 03:22 PM
It looks like the order of operation of the vectors should be reversed because you want the vector that is the forward direction which points at the target. So it should be Quaternion.LookRotation(transform.position-player.position). Just my best guess since you didn't give any specifics about what kind of behavior you did observe. So your mileage may vary. Best of luck!
Your answer
Follow this Question
Related Questions
Using raycast to determine the thickness of a collider 1 Answer
Access a specific element in GridLayoutGroup by script ? 1 Answer
How do I make the FPSController be compatible with UNET? 0 Answers
Are Animator bloking scriptbased Rotation? 0 Answers
Need little help with my Script i want to Add Min Height and Maxheight in ButtonScript 0 Answers