- Home /
Orbit Camera around object
I have this script (from the script reference) that makes the camera orbit my selected object when I use the joystick. However, it only moves for a few degrees, and 'snaps' back when I let go of the joystick.
All these Quaternions, Eulers, and Slerps still confound me. Can someone see what I need to do with this to make the camera keep orbiting?
using UnityEngine; using System.Collections;
public class JoystickOrbit: MonoBehaviour { public Transform target; // This is the controller we are following public float smooth = 2.0F; public float tiltAngle = 30.0F;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void LateUpdate() { //Every frame, do this as late as you can
float tiltAroundZ = Input.GetAxis("RightStickHorizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("RightStickVertical") * tiltAngle;
Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
}
The code entered was giving errors about missing semi-colons on lines 14, 15, and 19; I don't know why the semi-colons are there.
Anyway, since I don't know javascript, but I know a bit of C#, I converted the code to that, and got it working, but it does the same thing: camera rotates a few degrees only then snaps back.
Does anyone have any further ideas?
using UnityEngine; using System.Collections;
public class JoystickOrbit : $$anonymous$$onoBehaviour {
const float y$$anonymous$$inLimit = -360;
const float y$$anonymous$$axLimit = 360;
const float x$$anonymous$$inLimit = -360;
const float x$$anonymous$$axLimit = 360;
const float tiltAngle= 30.0f;
const float smooth = 2.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void LateUpdate() { //Every frame, do this as late as you can
float tiltAroundY = Input.GetAxis("RightStickVertical") * tiltAngle;
float tiltAroundX = Input.GetAxis("RightStickHorizontal") * tiltAngle;
tiltAroundY = ClampAngle(tiltAroundY, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
tiltAroundX = ClampAngle(tiltAroundX, x$$anonymous$$inLimit, x$$anonymous$$axLimit);
Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
private int ClampAngle (float angle, float $$anonymous$$, float max) {
if (angle < -360){
angle += 360;
}
if (angle > 360){
angle -= 360;
}
return $$anonymous$$athf.Clamp ((int)(angle), (int)($$anonymous$$), (int)(max));
}
}
It could possibly be a problem with $$anonymous$$onoDevelop. When you copy and paste code into it, sometimes there are 'invisible' characters in the code. Sometimes this can be fixed by removing indentations(select all, shift+tab), then highlighting and deleting the blank space after every line. (then indent everything again). Sometimes you have to create a new line and type it in again, then delete the line with the error completely.
@OldLadyNevermore: i think it because the special thing about Quaternion ( or rotation ). The line Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0); means it will create a new Quaternion with the same value as previous LateUpdate (if you continue hold the axis) or 0 if you release the axis.The code may change a bit into this: Quaternion multiply= Quaternion.Euler(tiltAroundY, tiltAroundX, 0); Quaternion target = transform.rotation*multiply; if you want to return the rotation to the normal, just make a variable that holds the normal rotation at the beginning (or just before rotate)
Sorry for that unclear comment but i think the Code Sample button not work for me. Hope you can get it.
I tried changing that section of code to read like this; but the issue persists:
Quaternion multiply= Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
Quaternion target = transform.rotation*multiply;
//Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
Answer by daivd.ramz · Jul 28, 2013 at 07:07 AM
hi, i don't work with joystick still but con you try this:
var yMinLimit:float = -360;
var yMaxLimit:float = 360;
var xMinLimit:float = -360;
var xMaxLimit:float = 360;
void LateUpdate() { //Every frame, do this as late as you can
float tiltAroundY = Input.GetAxis("RightStickHorizontal") * tiltAngle;
float tiltAroundX = Input.GetAxis("RightStickVertical") * tiltAngle;
tiltAroundY = ClampAngle(tiltAroundY, yMinLimit, yMaxLimit);
tiltAroundX = ClampAngle(tiltAroundX, xMinLimit, xMaxLimit);
Quaternion target = Quaternion.Euler(tiltAroundY, tiltAroundX, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360){
angle += 360;
}
if (angle > 360){
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
i hope this work for you :)
Answer by OldLadyNevermore · Jul 30, 2013 at 01:43 AM
Okay, with everyone's help, I was able to reverse-engineer the mouseorbit script and get it working with joysticks.
Here is the final result (turn on zoom to disable up-and-down in favor of zoom):
using UnityEngine; using System.Collections;
public class JoystickOrbit : MonoBehaviour { public Transform target; public float distance = 10.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float yMinLimit = -20f; public float yMaxLimit = 80f; public float zoomSpeed = 120.0f; private float x = 0.0f; private float y = .0f; public bool zoom;
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody) {
rigidbody.freezeRotation = true;
}
}
void LateUpdate () {
if (target) {
x += (float)(Input.GetAxis("RightStickHorizontal") * xSpeed * 0.02);
if (zoom) {
distance -= (float)(Input.GetAxis("RightStickVertical"));
}
else {
y += (float)(Input.GetAxis("RightStickVertical") * zoomSpeed * 0.02);
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
private int ClampAngle (float angle, float min, float max) {
if (angle < -360){
angle += 360;
}
if (angle > 360){
angle -= 360;
}
return Mathf.Clamp ((int)(angle), (int)(min), (int)(max));
}
}