How would I add rotation limits to this?
I have the following script that I use to have my character's head face the target. However an error has come up where I've noticed that I don't currently have any limits for the rotation and so the character's head will regularly recreate scenes from the exorcist in order to look at the target. How would I set max/min limits to the various axis?
using UnityEngine;
using System.Collections;
public class LookAtSlow : MonoBehaviour {
public Transform target;
public float degreesPerSecond;
// Update is called once per frame
void Update () {
// The difference between target position and our position yields a direction
Vector3 dirFromMeToTarget = target.position - transform.position;
// Calculates a rotation where the direction 'dirFromMeToTarget' would be forward
Quaternion lookRotation = Quaternion.LookRotation(dirFromMeToTarget);
// Lerp from our current rotation to the desired 'lookRotation' at a rate of 'degreesPerSecond'
transform.rotation =
Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * (degreesPerSecond/360.0f));
}
}
Answer by Sgt_Spike · Jul 18, 2018 at 09:14 PM
Hey there. I think a 'Clamp' might do the trick. Try having a look at this page here 'https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html' and see if that's what you're looking for :D
What would I need to do to bring that into my setup. Sorry I'm really new to C# scripting.
To be honest I've never really used $$anonymous$$athf.Clamp myself :D. I think using something like this 'transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, -180, 180);' would work. What this code here should hopefully do is clamp the 'x' value of 'this' (assu$$anonymous$$g 'this' is your player) between -180 and 180, meaning that it cannot ever equal 270 for example. You will of course need to change 'x' to which ever coordinate it is that tilts the head left and right (probably x or z), and -180 and 180 can be changed to whatever you want. I haven't tried this code myself so let me know if it works or not!
Okay so I tried putting that into the script but I'm pretty sure I put it in the wrong spot since I'm getting an error now, also the axis that I want to limit the rotation of is the Y axis; I'd also like to limit the x a bit so that the head doesn't collapse into his chest if I were directly below him.
using UnityEngine;
using System.Collections;
public class LookAtSlow2 : $$anonymous$$onoBehaviour {
public Transform target;
public float degreesPerSecond;
// Update is called once per frame
void Update () {
// The difference between target position and our position yields a direction
Vector3 dirFrom$$anonymous$$eToTarget = target.position - transform.position;
// Calculates a rotation where the direction 'dirFrom$$anonymous$$eToTarget' would be forward
Quaternion lookRotation = Quaternion.LookRotation(dirFrom$$anonymous$$eToTarget);
transform.position.y = $$anonymous$$athf.Clamp(transform.position.y, -180, 180);
// Lerp from our current rotation to the desired 'lookRotation' at a rate of 'degreesPerSecond'
transform.rotation =
Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * (degreesPerSecond/360.0f));
}
}
Your answer
Follow this Question
Related Questions
Why won't my model rotate up on X axis? 1 Answer
How to object rotate on a single axis towards another object? C# 2 Answers
Rotating Camera around Player's X-axis while rotating Player around its Y-axis (using mouse input) 0 Answers
How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers
how to reset back the rotation 0 Answers