- Home /
Rotate towards target with limitation on certain axis? c#
Ok so I have this c# script to get my enemy to look at my player, but how can I add to this script to restrict the Y axis rotation? I tried a few other scripts i found online but they didnt seem to work right. Thank you!
using UnityEngine;
public class rotating: MonoBehaviour
{
//values that will be set in the Inspector
public Transform Target;
public float RotationSpeed;
//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;
// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
_direction = (Target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}
i dont know much c# but if your enemy has a rigidbody you can select which axis it will be allowed/not allowed to rotate/translate on.
i posted a suggestion, but still pending... try :
void Update()
{
Vector3 a = transform.eulerAngles;
//find the vector pointing from our position to the target
_direction = (Target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
transform.eulerAngles.x = a.x;
transform.eulerAngles.z = a.z;
}
Answer by anonymousUser · Aug 24, 2012 at 03:09 PM
can you do this?
void Update()
{
Vector3 a = transform.eulerAngles;
//find the vector pointing from our position to the target
_direction = (Target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
transform.eulerAngles.x = a.x;
transform.eulerAngles.z = a.z;
}
Answer by Arshia001 · Aug 24, 2012 at 02:32 PM
try adding this after _lookRotation = ...
Vector3 v = _lookRotation.eulerAngles;
v.x = 0;
_lookRotation.eulerAngles = v;
this code sets the X rotation of the euler angles to zero, which means you'll get no rotation around X. Since the quaternion is looking at your player/object/whatever, the Z axis is pointing in that way and if you remove the rotation around X, the enemy will stay upright. If you want to restrict another axis, change the x to whatever you need.
Idk for some reason the enemy is really glitchy, to the point where if I touch him I go flying across the map. Is it that the nav mesh agent component does it's own rotation? This is the pro version, but the AI following on it was really bad, the enemy would slide around and take forever to turn towards me, so I wanted to have him face me with this rotation script but nothing seems to work, it only makes him glitch more.
Ok I got it, the nav mesh agent component's built in rotation suck but it must have interfered with my c# script. So I just had to adjust some variables to the nav mesh agent for it to rotate faster, and the Y axis is never affected. I wish it was more customizable, but the AI navigation is really good compared to anything else I've used. Thanks for all the help you guys I'm sure those scripts work just not at the same time as another one, which probably caused the glitching.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trouble with Vector3.Angle() and RotateAround() 1 Answer
Distribute terrain in zones 3 Answers
Rotating object around Z axis with lerp/slerp 1 Answer
Rotate Animation In-Game? 2 Answers