• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pixzle · Nov 29, 2015 at 10:56 AM 0
Share

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 (:

avatar image Karmate Pixzle · Nov 29, 2015 at 02:51 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS cam for Roll -a-Ball like game 1 Answer

Camera leaning problems (Lerping) 0 Answers

Slight camera stutter with Cinemachine 2D (Video inside) 1 Answer

Specific camera coordinates based on players coordinates. 1 Answer

[SOLVED] Third Person Spherically Orbiting Camera 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges