I have animator controller that is enabled and I want it to keep enabled.
I also have a script that is attached to a camera that shoot raycast and then if it’s hitting specific item/s I did that the player head rotating smooth looking at the item/s.
The problem is when the animator controller is enabled the head will not rotating looking at the tiem.
Only if I will disable the animator controller then the head will rotate fine.
The head is rotating by the animator controller and I want to keep the head also rotating by the animator controller but also to rotate looking at the item/s.
I’m not sure but one solution maybe will be using LateUpdate for the head rotation while the raycast will keep being shooting in FixedUpdate but I’m not sure how to do it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
public Transform objToRotateLookAT;
public float lookAtSpeed;
public float originalLookSpeed;
public float distanceToInteract;
public Animator anim;
private bool raycastSucceed;
private Quaternion originalRotation;
private bool originalRotFlag = false;
// Start is called before the first frame update
void Start()
{
originalRotation = objToRotateLookAT.rotation;
if (anim == null)
{
anim = GetComponent<Animator>();
}
}
void FixedUpdate()
{
int layerMask = 1 << 8;
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
if (!raycastSucceed)
Debug.Log("Did Hit");
raycastSucceed = true;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
RotateGameObject(hit.transform.position, objToRotateLookAT);
if(Vector3.Distance(transform.position, hit.transform.position) <= distanceToInteract)
{
anim.SetBool("Pickup Item", true);
}
}
else
{
if (raycastSucceed)
Debug.Log("Did not Hit");
raycastSucceed = false;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.yellow);
if (objToRotateLookAT.rotation != originalRotation && originalRotFlag == false)
{
objToRotateLookAT.rotation = Quaternion.Lerp(objToRotateLookAT.rotation, originalRotation, originalLookSpeed * Time.deltaTime);
originalRotFlag = true;
//anim.SetBool("Grounded", false);
}
else
{
//anim.SetBool("Grounded", true);
}
}
}
private void LateUpdate()
{
}
private void RotateGameObject(Vector3 target, Transform objToRotate)
{
Vector3 targetDirection = target - objToRotate.position;
// The step size is equal to speed times frame time.
float singleStep = lookAtSpeed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(objToRotate.forward, targetDirection, singleStep, 0.0f);
// Draw a ray pointing at our target in
//Debug.DrawRay(objToRotate.position, newDirection, Color.red);
// Calculate a rotation a step closer to the target and applies rotation to this object
objToRotate.rotation = Quaternion.LookRotation(newDirection);
}
}
If there was a hit I’m calling the method RotateGameObject and if there is no hit and the player head is not at his last original rotation then I’m Lerp to rotate the head back Quaternion.Lerp.
The problem is that the rotating child transforms that the parent have enabled animator will not working only if the animator is disabled and I don’t want to disable the animator.