- Home /
Move camera around object IOS
Hello guys! well I have been trying to figure this out for quite some time but for some reason I can´t get it working...
This is the thing I have already a mouse Orbit Script from someone at Unity Forums (can´t remember who) it works perfectly! and I want to do the same one for the iphone but it must work with finger swipes (I already added some code to detect them). Can you guys help me with the RotateAround part?
Any ideas?
Here is the mouse Orbit code, it is pretty simple:
var target : Transform;
var distance = 10.0;
var cameraSpeed = 5;
var xSpeed = 175.0;
var ySpeed = 75.0;
var yMinLimit = 30; //Lowest vertical angle in respect with the target.
var yMaxLimit = 80;
var minDistance = 5; //Min distance of the camera from the target
var maxDistance = 20;
private var x = 0.0;
private var y = 0.0;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target && camera) {
//Zooming with mouse
distance += Input.GetAxis("Mouse ScrollWheel")*distance;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
//Detect mouse drag--> PC input;
if(Input.GetMouseButton(0)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
}//Left
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
transform.rotation = rotation;
}
//Detect swipes--> iPhone Input
for (var evt: iPhoneTouch in iPhoneInput.touches){
if(evt.phase == iPhoneTouchPhase.Moved ){
//Here is where I need to do the exact same thing that was done with the mouse;
}
}
}
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);
}
Answer by GameFreak · Dec 29, 2011 at 08:44 PM
I can write down the code for you if you say.
But I would rather suggest just import the Standard Assets (Mobile) and use the prefabs / scripts to do your job.
If youre using scripts tell me I will tell you how to make the joystick and use it.
--Rishab
I can use the Standard Assets ($$anonymous$$obile) but that´s not what I am looking for. I want something like the right joystick but with finger Swipes.
Finger swipes? Use for (var touch : iPhoneTouch in iPhoneInput.touches) { if (touch.phase == iPhoneTouchPhase.$$anonymous$$oved)
{
var FingerDid$$anonymous$$ove=true;
}
}
If you want to drag an object google it :)
Finger swipes? Use for (var touch : iPhoneTouch in iPhoneInput.touches) { if (touch.phase == iPhoneTouchPhase.$$anonymous$$oved)
{
var FingerDid$$anonymous$$ove=true;
}
}
If you want to drag an object google it :)
Answer by BalsamicVinegar · Feb 01, 2012 at 07:03 AM
More of a head start than an aswer, but this should work for your swipe detection and your Y axis rotation. X axis is a little more complex and I'm a bit too busy at the moment to figure it out. But I'm sure the mouse orbit script has a good solution.
Vector2 orbit = Vector2.zero;
if(Input.touches.Length > 0) {
//Just grab the first touch
if(Input.touches[0].phase == TouchPhase.Moved) {
//If it's moving, grab the amount of movement
orbit = Input.touches[0].deltaPosition;
}
}
if( orbit != Vector2.zero) {
//Ok, so if there is movment then we rotate the camera around the player
//I'm going to assume you have four variables here.
//1. the target transform - I'll call this _target
//2. The distance the camera is from the target - I'll call this _distance
//3. A Vector2 with the current amount of rotation you have given the camera - I'll call this _currentRotation
//4. A reference to the camera object - I'll call this _camera ..... IF this is attached to the camera object, just use gameObject.transform instead
//First move the camera to the target
_camera.position = _target.position + (_target.forward * _distance);
//Now add the movement onto the _currentRotation variable
_currentRotation.x += orbit.x * Time.deltaTime;
_currentRotation.y += orbit.y * Time.deltaTime;
//This works fine for rotation around the y axis.
_camera.RotateAround(_target.up, _currentRotation.x);
//Now to rotate on X it is quite awkward. Just using the _target.right will cause issuses when you rotate on y
//You will probably need to split the 360 degree rotation into 90 degree quadrants and figure out which axis is appropriate
}
Your answer
Follow this Question
Related Questions
Look Behind Me While Still Moving Forward 1 Answer
Making an object rotate around a sphere 1 Answer
Camera orbit around clicked position 0 Answers
unity2D: make object face mouse 2 Answers
Trouble with moving the camera. 2 Answers