Hi guys. I am trying to make a script where my character smoothly looks at a specific raycast point. I already have the character able to look at the transform of an OBJECT but, I really want to make it so that it does the same thing with a raycast point.
Here is the script that I am using to accomplish this. Note that the “look at obj” part of this works as desired, I just want to make the “look at floor” part to also work using the method used for the “look at obj” part:
So, rather than using transform.LookAt to look at the raycast hit point, I’d like to use the Quaternion.LookRotation method (as I already have the smooth-look code working for that method)
var damping = 5.0;
var xConstraint = 360;
var yConstraint = 20;
function Update () {
if ( getTarget.targetObject){
var currentObject:GameObject = getTarget.targetObject;
var currentObjectName = getTarget.targetObject.name;
var currentDest = getTarget.targetLocation;
var target = getTarget.targetObject.transform;
}
if (target) {
var rot = Quaternion.LookRotation(target.position - transform.position);
if (currentObjectName == "floor"){
print("look at floor");
var lookPos = Vector3(currentDest.x, 5, currentDest.z);
transform.LookAt(lookPos);
}
else {
print ("look at obj");
rot = Quaternion.LookRotation(target.position - transform.position);
var rotationX = rot.eulerAngles.y;
var rotationY = rot.eulerAngles.x;
rotationX = ClampAngle(rotationX, -xConstraint, xConstraint);
rotationY = ClampAngle(rotationY, -yConstraint, yConstraint);
rotationY = -rotationY;
var xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
var yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
transform.localRotation = Quaternion.Slerp(transform.localRotation, xQuaternion * yQuaternion, Time.deltaTime * damping);
// Trying to force roll to 0
transform.eulerAngles.z = 0;
}
}
}
function ClampAngle(angle, min, max){
var localMin:int = min;
var localMax:int = max;
var localAngle:int = angle;
if (1){
if (localMin < 0) localMin += 360;
if (localAngle > 180){
return Mathf.Clamp(localAngle, localMin, 360);
} else {
return Mathf.Clamp(localAngle, 0, localMax);
}
}
}