- Home /
camera orbit like star defence
Hi folks!
How Can I do a mouse orbit like this?
http://www.youtube.com/watch?v=OouBzirUZww&feature=related
The mouse orbit in the standard assets is limited :|, when I'm at the top of the sphere the camera spins!
Tnx a lot :)
Answer by Keeblebrox · Mar 20, 2011 at 02:26 PM
Yeah, I could understand you'd have issues using mouse orbit. I suggest you combine Transform.LookAt( Vector3 ), to keep the camera looking at your target, with regular movement scripting.
In untested C#
using UnityEngine; using System.Collections;
public class FreeformMouseOrbit : MonoBehaviour { public Transform target; public float desiredDistance = 10.0f;
// should be used for all camera-movement scripts
public void LateUpdate()
{
// focus camera on target
transform.LookAt( target.position );
// ... use science to figure out direction of travel relative to camera (up, right, down, left, or a combination)
Vector3 movementVector = science;
// move in that direction
transform.position += movementVector;
// if you have problems with zoom, move the camera so that it's the appropriate distance from the target
float actualDistance = Vector3.Distance( transform.position, target.position );
if( !Mathf.Approximately( desiredDistance, actualDistance ) )
{
Vector3.MoveTowards( transform.position, target.position, Mathf.Abs( actualDistance - desiredDistance ) );
}
}
}
Now in untested JS:
// FreeformMouseOrbit.js public var target : Transform; public var desiredDistance : float = 10.0;
// should be used for all camera-movement scripts public function LateUpdate() : void { // focus camera on target transform.LookAt( target.position );
// ... use science to figure out direction of travel relative to camera (up, right, down, left, or a combination)
var movementVector : Vector3 = science;
// move in that direction
transform.position += movementVector;
// if you have problems with zoom, move the camera so that it's the appropriate distance from the target
var actualDistance : float = Vector3.Distance( transform.position, target.position );
if( !Mathf.Approximately( desiredDistance, actualDistance ) )
{
Vector3.MoveTowards( transform.position, target.position, Mathf.Abs( actualDistance - desiredDistance ) );
}
}
Hi tnx for the reply :)
Science it's a mistery for me and I code only in js so I can't go throw this :)...
I added a JS version of the code if that helps. As for science, I left that part vague as it's up to your control scheme.
One possible method, if you're developing with the iPhone in $$anonymous$$d, would be to subtract the current touch point from the previous touch point and use the resulting vector (science = previousTouchPoint - currentTouchPoint). I'm not familiar enough with the iPhone API to give you code for that, though. I suggest going over the info at http://unity3d.com/support/documentation/ScriptReference/Touch.html
science
is undefined, making this answer unhelpful.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113822im_/https://answers.unity.com/themes/thub/images/avi.jpg)