• 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 GrimExp · Aug 13, 2018 at 08:38 AM · charactercontrollercamera rotatecamera-lookcamera rotationcamera.main

need a little help with my code camera rotation

so i'm working on the top down game for android but can't seem to get the camera to rotate on swipe from left to right can you guys help me with this because environment blocks the players video so i want them to be able to rotate the camera from right to left with swipe to be able to see in the direction they're heading i have tried so many different way to make this work but everything i've tried failed so can't you help me fix this

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class FollowCamera : MonoBehaviour
 {
     private Transform cacheTransform;
     public Transform CacheTransform
     {
         get
         {
             if (cacheTransform == null)
                 cacheTransform = GetComponent<Transform>();
             return cacheTransform;
         }
     }
 
     // The target we are following
     public Transform target;
     public Vector3 targetOffset;
     [Header("Follow")]
     public float damping = 10.0f;
     public bool dontSmoothFollow;
     [Header("Look at")]
     public float lookAtDamping = 2.0f;
     public bool dontSmoothLookAt;
     [Header("Rotation")]
     public float xRotation;
     public float yRotation;
     [Tooltip("If this is TRUE, will update Y-rotation follow target")]
     public bool useTargetYRotation;
     [Header("Zoom")]
     public float zoomDistance = 10.0f;
     [Header("Zoom by ratio (Currently work well with landscape screen)")]
     public bool zoomByAspectRatio;
     public float zoomByAspectRatioWidth;
     public float zoomByAspectRatioHeight;
     public float zoomByAspectRatioMin;
 
     private void Update()
     {
         var targetPosition = target == null ? Vector3.zero : target.position;
         var targetYRotation = target == null ? 0 : target.eulerAngles.y;
         var wantedPosition = targetPosition + targetOffset;
         var wantedYRotation = useTargetYRotation ? targetYRotation : yRotation;
 
         // Convert the angle into a rotation
         var currentRotation = Quaternion.Euler(xRotation, wantedYRotation, 0);
 
         // Set the position of the camera on the x-z plane to:
         // distance meters behind the target
         wantedPosition -= currentRotation * Vector3.forward * zoomDistance;
 
         // Update position
         if (!dontSmoothFollow)
             CacheTransform.position = Vector3.Slerp(CacheTransform.position, wantedPosition, damping * Time.deltaTime);
         else
             CacheTransform.position = wantedPosition;
 
         // Always look at the target
         if (!dontSmoothLookAt)
         {
             Quaternion lookAtRotation = Quaternion.LookRotation(targetPosition + targetOffset - CacheTransform.position);
             CacheTransform.rotation = Quaternion.Slerp(CacheTransform.rotation, lookAtRotation, lookAtDamping * Time.deltaTime);
         }
         else
             CacheTransform.rotation = Quaternion.LookRotation(targetPosition + targetOffset - CacheTransform.position);
 
         if (zoomByAspectRatio)
         {
             var targetaspect = zoomByAspectRatioWidth / zoomByAspectRatioHeight;
             var windowaspect = (float)Screen.width / (float)Screen.height;
             var scaleheight = windowaspect / targetaspect;
             var diffScaleHeight = 1 - scaleheight;
             if (diffScaleHeight < zoomByAspectRatioMin)
                 diffScaleHeight = zoomByAspectRatioMin;
             zoomDistance = diffScaleHeight * 20f;
         }
     }
 
     private void LateUpdate()
     {
 #if UNITY_EDITOR
         // Update camera when it's updating edit mode (not play mode)
         if (!Application.isPlaying && Application.isEditor)
             Update();
 #endif
     }
 }

Comment
Add comment · Show 1
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 GrimExp · Aug 13, 2018 at 04:02 PM 0
Share

please help me

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GrimExp · Aug 13, 2018 at 06:22 PM

wow though i was going to get help quick but i was wrong

Comment
Add comment · Share
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

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

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Unity’s FPSController camera 2 Answers

third person camera 2 Answers

Camera movement according to rotation and actual position RTS 1 Answer

camera zoom 1 Answer

Camera rotation 0 Answers

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