• 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
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

People who like this

0 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Third person camera gets closer to the player 0 Answers

Smooth camera on moving platform 1 Answer

Camera Follow a Target, but not Centered on it 1 Answer

Why is cinemachine Free Look zooming in when my character walks 0 Answers

Unity Camera Issue: Scroll up and down speed not consistent 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges