- Home /
MoveTowards rotation not always functioning
So I have as script where the player clicks on a target and the camera moves and rotates to focus the object clicked, the camera moves to the correct position but not the correct rotation each time. Occasionally when the target is clicked, the camera begins spinning in place and not going to the correct rotation. Before using MoveTowards I had Slerp and Lerp but neither worked with Raycast for some reason. Thank you in advance. Here is my script:
void Raycast()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
bool isHit;
isHit = false;
}
if (hit.collider != null && hit.collider.tag == "Human")
{
onHuman = true;
//cam.transform.LookAt(target);
cam.eulerAngles = Vector3.MoveTowards(cam.eulerAngles, goal.eulerAngles, Time.deltaTime * 500);
print("CLICCKKKK");
//if (hit.collider.tag == "Human")
// {
print("click");
GoToAbout();
Debug.Log("Target Position: " + hit.collider.gameObject.transform.position);
//}
}
}
void Update()
{
cam.position = Vector3.MoveTowards(cam.position, goal.position, Time.deltaTime * 100f);
// print(goal.eulerAngles)
//cam.eulerAngles
//cam.transform.Rotate(24,180,0);
// cam.eulerAngles = Vector3.Slerp(cam.eulerAngles, goal.eulerAngles, Time.deltaTime * 500);
if (Input.GetMouseButtonDown(0))
{
Raycast();
}
if (onHuman)
{
controller.GetComponent<FirstPersonController>().enabled = false;
countdown -= Time.deltaTime;
print(countdown);
if (countdown <= 0)
{
Debug.Log("main");
GoToMain();
controller.GetComponent<FirstPersonController>().enabled = true;
countdown = 5;
onHuman = false;
}
}
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Lerp Rotation C# 3 Answers
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer
Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers