- Home /
Moving Camera on player stop then move and rotate to separate point?
Currently I have a player that moves at a constant rate with the camera as a child object. I want to have it so that when I click a certain target object, the camera moves and is positioned to face the targeted object. Right now it moves to the specified position in front of the target when clicked, but does not rotate to face the target. Instead the camera begins spinning or faces a random incorrect direction. Here is my current script and thank you for all help:
using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour
{
//Handling
public Transform main;
// public Transform select;
public Transform cam;
public GameObject about;
//public Transform survival;
//Internal
Transform goal;
float countdown = 5;
bool onHuman = false;
GameObject controller;
public Transform target;
void Start()
{
//cam = GameObject.FindWithTag("MainCamera").transform;
// main = GameObject.FindWithTag("Main").transform;
controller = GameObject.FindWithTag("Control");
//about = GetComponentInChildren<GameObject>().transform;
Time.timeScale = 1.0f;
goal = main;
}
public void GoToMain()
{
goal = main;
}
public void GoToAbout()
{
goal = about.transform;
}
/*public void GoToSelectCampaign()
{
goal = select;
}
public void GoToSelectSurvival()
{
goal = survival;
}*/
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;
}
}
}
}
Comment