- Home /
how to limit axis of rotation when looking to a target
hi there,i'm new to C# andi just would like to ask ways on how to limit the axis of a rotation when targeting an object.i tried some methods solving this issues such as Mathf.Clamp and all but i don't quite understand how locking an axis works.
Here's an attached GIF of what my problem is.
public class targetFinder : MonoBehaviour {
public Transform target; public float turn_speed; public Quaternion limiter;
void Update () {
rotateTowards(target.position);
}
public void rotateTowards(Vector3 to)
{
Quaternion _lookRotation = Quaternion.LookRotation((to - transform.position).normalized);
if (_lookRotation.x == limiter.x)
_lookRotation.x = limiter.x;
if (_lookRotation.y == limiter.y)
_lookRotation.y = limiter.y;
if (_lookRotation.z == limiter.z)
_lookRotation.z = limiter.z;
if (_lookRotation.w == limiter.w)
_lookRotation.w = limiter.w;
transform.rotation =
Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * turn_speed);
transform.rotation = _lookRotation;
}
}
Answer by J-F · Jul 16, 2018 at 09:17 AM
Take a look at Mathf.clamp: https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
Here's an example: https://answers.unity.com/questions/659932/how-do-i-clamp-my-rotation.html
I'm guessing that mathf.clamp didn't work for you because you can't directly clamp transform.rotation. You need to clamp the values you intend to set as the rotation.
Answer by jcook7595 · Feb 21, 2019 at 05:37 AM
Allassignmenthelp is a web portal where students get assignment help for all the subjects, with the help of our experts. You will get 100% plagiarism free assignment. Expert’s consultation is also available for students. If they have any query they can contact with our experts anytime.