- Home /
How to make Touch Control Smooth
Hello there I really could use some help with this!
I have found this bit of code and it works perfectly with what I want to do, But when I Touch move the camera, it isnt smooth.
How would I be able to make the camera smoothly move when I use touch.
This is the code:
#pragma strict
var target : Transform; var distance = 20.0; var wheelSpeed = 10.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
var minDistance = 5.0;
var touch : Touch; var curDist = 0.0; var lastDist = 0.0; var pinchSpeed : float ; var cameraSpeed =1.0;
private var x = 0.0; private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles= transform.eulerAngles;
x = angles.y;
y = angles.x;
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {
if (target && GetComponent.<Camera>()) {
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02f;
y -= touch.deltaPosition.y * ySpeed * 0.02f;
}
if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
{
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
curDist = Vector2.Distance(touch1.position, touch2.position);
if(curDist > lastDist)
{
distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}
else
{
distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}
lastDist = curDist;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation= Quaternion.Euler(y, x, 0);
var vTemp = new Vector3(0.0f, 0.0f, -distance);
var position= rotation * vTemp + target.position;
transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
transform.rotation = rotation;
}
}
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); }
Thank you, any help would be great!
Answer by fred_gds · Mar 13, 2016 at 09:00 PM
You would need to use something like Lerp() to smoothen the camera position/angle over a given timeframe.
Thank you for the answer! I was just looking at that. I don't know how to implement it into the code though!
Well you already use Lerp for the position:
transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
So if you would that to be smoother you would decrease the camera Speed.
And then you would do the same thing for transform.rotation except that here you need to use Quaternion.Lerp()
Your answer
Follow this Question
Related Questions
Best practice for tiled 3D terrain 0 Answers
Unity 4 animations 0 Answers
3d Models, How to turn invisible 3 Answers
Adding force to a bullet 3 Answers