- Home /
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
}
}
Answer by GrimExp · Aug 13, 2018 at 06:22 PM
wow though i was going to get help quick but i was wrong
Your answer
Follow this Question
Related Questions
third person camera 2 Answers
Camera movement according to rotation and actual position RTS 1 Answer
camera zoom 1 Answer
Camera rotation 0 Answers
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers