- Home /
Mouse orbit + Smooth follow script question
Hi,
I'm using the built in Mouse orbit + Smooth follow Unity scripts, these two are working fine except for one thing. If I click and drag/rotate so the camera rotates around my object, the smooth follow script always makes the camera go back to the original position if I let go of the mouse button. The smooth follow script only seems to want the camera to be in one position if no mouse input is given..and follow the given game object like that.. Is there a way to remember where the current position/angle/rotation is and have the camera stay in that position and hive it follow using this until I click drag again and rotate...
Anyone come across this before or know of a hack/fix?
Cheers, Kim
You will have to modify the scripts. If it were me, I'd rewrite the two scripts into a single script, but you might be able to get by with a careful mod. One approach is to add a 'offsetAngle' as a public variable to the SmoothFollow script. This value is used to modify the calculation of 'wantedRotation' on line 32. Then in the $$anonymous$$ouse Orbit script, you would set 'offsetAngle' to the same value as 'x'.
Hi robertbu, many thanks for your quick help, I'll look into this, and post the script if/once I succeed! good idea about the public variable. Will also need to figure out how to use a public variable, is it as simple as defining this var as public and then it'll be seen in all scripts? I think so...
95% there now... In SmoothFollow.js I added/modified: var myval : GameObject; function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles myval = GameObject.Find("$$anonymous$$ain Camera"); var wantedRotationAngle = myval.GetComponent($$anonymous$$ouseOrbit).y; //var wantedRotationAngle = target.eulerAngles.y;
So adding this line: var wantedRotationAngle = myval.GetComponent($$anonymous$$ouseOrbit).y; was the important one, as robertbu suggested.
In $$anonymous$$ouseOrbit.js I had added code to only orbit on mousedown (if (Input.Get$$anonymous$$ouseButton(0)) {) but I could not get the code to work nicely until I removed this line in function LateUpdate (), so now my code code looks like:
function LateUpdate () { // if (Input.Get$$anonymous$$ouseButton(0)) {
if (target) { x += Input.GetAxis("$$anonymous$$ouse X") xSpeed 0.02; y -= Input.GetAxis("$$anonymous$$ouse Y") ySpeed 0.02; y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit); var rotation = Quaternion.Euler(y, x, 0); var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; transform.rotation = rotation; transform.position = position; // } }
So it works well now but not sure how I can get the Input.Get$$anonymous$$ouseButton(0) to work again in mouseorbit...I tried moving that line around to several places in the LateUpdate () function but no help.. any ideas anyone?
Thanks Robertbu for your help with this one again.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Control camera target from another scripts 1 Answer
2.5d camera control script 1 Answer
Camera Control Help 0 Answers
character switching on collision 1 Answer