Set Rotation
Hello, I am writing a C# script to make a farmer face a gameobject. However, I only want the farmer to pivot on the y axis. So far my script is:
using UnityEngine;
using System.Collections;
public class choose_mole : MonoBehaviour {
private static int recieverclosestsq;
public Transform target;
public Vector3 relativepos;
void Update ()
{
recieverclosestsq = molepos.closestsq;
Vector3 relativePos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
}
I can't find anything useful that locks the x and z rotations. Please send in a detailed explanation if you can. Thank you for the help!
Answer by ZeraTFK · Nov 15, 2015 at 04:36 PM
Use a new Vector for your target vector with the same Y as the Farmer.
Vector3 nTarget = new Vector3(target.position.x, transform.position.y, target.position.z);
Ant you could use transform.LookAt()
Vector3 nTarget = new Vector3(target.position.x, transform.position.y, target.position.z);
transform.LookAt(nTarget);
@ZeraTF$$anonymous$$ Could you show me the full code? I can't implement it.
This is all! But i created a Example 4U
There u has a player gameobject and a target. Start the Game and move the target in the Scene Editor
Your answer
Follow this Question
Related Questions
Rotate a point around a mesh vertex 1 Answer
No Particle Rotation When Setting rotation3D from C# 0 Answers
Fix rotation skipping 0 Answers
How to make platform to rotate player ? 0 Answers
Is it possible to lock file in Unity ? 0 Answers