- Home /
Question by
Karmate · Nov 29, 2015 at 09:59 AM ·
camera followcamera movementsmoothfollow
How to Smooth Camera Follow script for Android?
I am really stuck here. Camera looks really weird in Android. I can't smooth it. Can you check script please?
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class CameraFollow : MonoBehaviour
{
public Transform target; // The target Transform to follow
public float followSpeed; // Smooth follow speed
public float distance; // The current distance to target
public float zoomSpeed; // The speed of interpolating the distance
public float zoomSensitivity; // The sensitivity of mouse zoom
public float rotationSensitivity; // The sensitivity of rotation
public float yMinLimit; // Min vertical angle
public float yMaxLimit; // Max vertical angle
public Vector3 offset; // The offset from target relative to camera rotation
public float x { get; private set; } // The current x rotation of the camera
public float y { get; private set; } // The current y rotation of the camera
public float distanceTarget { get; private set; } // Get/set distance
private Vector3 targetDistance, position;
private Quaternion rotation = Quaternion.identity;
private Vector3 smoothPosition;
protected virtual void Awake ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
distanceTarget = distance;
smoothPosition = transform.position;
}
protected virtual void FixedUpdate ()
{
UpdateTransform (Time.deltaTime);
}
public void UpdateTransform (float deltaTime)
{
distance += (distanceTarget - distance) * zoomSpeed * deltaTime;
rotation = Quaternion.AngleAxis (x, Vector3.up) * Quaternion.AngleAxis (y, Vector3.right);
smoothPosition = Vector3.Lerp (smoothPosition, target.position, deltaTime * followSpeed);
position = smoothPosition + rotation * (offset - Vector3.forward * distance);
transform.position = position;
transform.rotation = rotation;
}
protected virtual void LateUpdate ()
{
UpdateInput ();
}
public void UpdateInput ()
{
#if MOBILE_INPUT
x += CrossPlatformInputManager.GetAxis ("Mouse X") * rotationSensitivity;
y = ClampAngle (y - CrossPlatformInputManager.GetAxis ("Mouse Y") * rotationSensitivity, yMinLimit, yMaxLimit);
#else
if (Input.GetMouseButton (1)) {
x += Input.GetAxis ("Mouse X") * rotationSensitivity;
y = ClampAngle (y - Input.GetAxis ("Mouse Y") * rotationSensitivity, yMinLimit, yMaxLimit);
}
#endif
}
private float ClampAngle (float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
public void SetZoom(float f)
{
distanceTarget = f;
}
}
Comment
You should give more information, explain how the script is supposed to act and how it's acting. It's hard to help with a 100 line script thrown in your face without explanation (:
I think it is a rotation problem. I tried lerp and slerp to smooth rotation, but it did not fix.
I don't know true word for my problem in English. It is like game is enforcing the device but it does not. I control camera with unity crossplatfrom's "pad". When i move my finger on pad, camera is moving piece and piece not smooth.
I hope someone get my problem and help me :D