- Home /
how to stop making script constantly want to look at the vector
Ok so I made a camera script that will pan over whatever you right click on but if i move the camera the script still wants to look at that spot so makes panning the camera impossible. How to I make it stop caring once it has looked there? I tried putting it in the right click statement which works but it never finishes the rotation to where I clicked which is its own issue.
Here is my JS script so far:
#pragma strict
var rotationSpeed: float = 5;
var target : Transform;
var hit : RaycastHit;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
}
function Update () {
if(Input.GetMouseButton(1)) {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
Debug.Log("Mouse Location " + hit.point);
Debug.Log("camera Location " + Camera.mainCamera.gameObject.transform.position);
Debug.Log("mouse coord X =" + Input.GetAxis("Mouse X"));
Debug.Log("camera Rotation" + Camera.mainCamera.transform.rotation);
//roatation system working if points are stored and start point is created when script load for starting position for each map.
}
Camera.mainCamera.transform.rotation = Quaternion.Slerp(Camera.mainCamera.transform.rotation, Quaternion.LookRotation(hit.point - Camera.mainCamera.gameObject.transform.position), rotationSpeed*Time.deltaTime);
}
Answer by robertbu · May 26, 2013 at 02:12 PM
You need to put in a flag and turn off the tracking when the camera is pointed in the right direction. I've used 'tracking' for the boolean flag these mods to your code:
#pragma strict
var rotationSpeed: float = 5;
var target : Transform;
var hit : RaycastHit;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
private var tracking = false;
function Update () {
if(Input.GetMouseButton(1)) {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
Debug.Log("Mouse Location " + hit.point);
Debug.Log("camera Location " + Camera.mainCamera.gameObject.transform.position);
Debug.Log("mouse coord X =" + Input.GetAxis("Mouse X"));
Debug.Log("camera Rotation" + Camera.mainCamera.transform.rotation);
tracking = true;
//roatation system working if points are stored and start point is created when script load for starting position for each map.
}
if (tracking) {
var q = Quaternion.LookRotation(hit.point - Camera.mainCamera.gameObject.transform.position);
Camera.mainCamera.transform.rotation = Quaternion.Slerp(Camera.mainCamera.transform.rotation, q, rotationSpeed*Time.deltaTime);
if (Quaternion.Angle(Camera.mainCamera.transform.rotation, q) < 0.1) {
tracking = false;
Debug.Log("Tracking Done");
}
}
}
This is more what im looking for now a few adjustments and i should get it to work exactly how i need. Thanks!
Answer by NineSpin · May 26, 2013 at 02:28 PM
hm... maybe because you are telling the camera to rotate towards this point constantly (it is un update function), you should simply move line 25. into the if statement of your function as so:
if(Input.GetMouseButton(1)) {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit))
Debug.Log("Mouse Location " + hit.point);
Debug.Log("camera Location " + Camera.mainCamera.gameObject.transform.position);
Debug.Log("mouse coord X =" + Input.GetAxis("Mouse X"));
Debug.Log("camera Rotation" + Camera.mainCamera.transform.rotation);
//roatation system working if points are stored and start point is created when script load for starting position for each map.
Camera.mainCamera.transform.rotation = Quaternion.Slerp(Camera.mainCamera.transform.rotation, Quaternion.LookRotation(hit.point - Camera.mainCamera.gameObject.transform.position), rotationSpeed*Time.deltaTime);
}
so your camera rotates only when clicked.
This will not work. I did state that i tried this and it does not finish tracking the camera.
Your answer
Follow this Question
Related Questions
RTS Camera Rotation and Movement 0 Answers
movement disable when camera changes view 1 Answer
Move camera forward/backwards with mouse wheel 1 Answer
Final Fantasy style camera 1 Answer
Constraining a camera movement 0 Answers