Hey all, I am making a game in which I want my camera to rotate 360 degree around the cube at the center. I am using this script.
public class CameraMovement : MonoBehaviour
{
public Transform TargetLookAt; //target to which camera will look always
public float Distance=10f; //distance of camera from target
public float DistanceMin=10f; //min distance of camera from target
public float DistanceMax=50f; //max distance of camera from target
public float DistanceSmooth=0.05f; //smoothing factor for camera movement
public float X_Smooth=0.05f;
public float Y_Smooth=0.1f;
public float X_Sensitivity=30f;
public float Y_Sensitivity=30f;
private float touch_dist_x;
private float touch_dist_y;
private Vector2 touchDeltaPosition;
public float MouseWheelSensitivity=5f;
public float Y_MinLimit=-40f;
public float Y_MaxLimit=80f;
private float startDistance=0f; //Validated start distance
private float desiredDistance=10f; //desired distance of camera from target
private float velDistance=0f; //
private Vector3 desiredPosition=Vector3.zero; //
private float velX=0f;
private float velY=0f;
private float velZ=0f;
private Vector3 position=Vector3.zero;
void start()
{
//Distance=Mathf.Clamp(Distance,DistanceMin,DistanceMax); //check and validate min<distance<max
Distance=34f;
startDistance=Distance;
}
void LateUpdate ()
{
if(!Tile.cubeTouch)
{
if(TargetLookAt==null)
return;
HandlePlayerInput();
CalculateDesiredPosition();
UpdatePosition();
}
}
void HandlePlayerInput()
{
if(Input.GetMouseButton(0))
{
//touchDeltaPosition = Input.GetTouch(0).deltaPosition;
touch_dist_x+= Input.GetAxis("Mouse X")*X_Sensitivity;
touch_dist_y-= Input.GetAxis("Mouse Y")*Y_Sensitivity;
currentAngle=this.transform.eulerAngles.y;
}
}
void CalculateDesiredPosition()
{
Distance=Mathf.SmoothDamp(Distance,desiredDistance,ref velDistance,DistanceSmooth);
desiredPosition=CalculatePosition(touch_dist_y,touch_dist_x,Distance);
}
Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
{
Vector3 direction=new Vector3(0,0,-distance);
Quaternion rotation=Quaternion.Euler(rotationX,rotationY,0);
return TargetLookAt.position+rotation*direction;
}
void UpdatePosition()
{
var posX=Mathf.SmoothDamp(position.x,desiredPosition.x,ref velX,X_Smooth);
var posY=Mathf.SmoothDamp(position.y,desiredPosition.y,ref velY,Y_Smooth);
var posZ=Mathf.SmoothDamp(position.z,desiredPosition.z,ref velZ,X_Smooth);
position=new Vector3(posX,posY,posZ);
transform.position=position;
transform.LookAt(TargetLookAt);
}
}
This script has problem. When my camera goes “Up side Down” ie behind the cube it flips the view. It looks like my mouse’s upward movement flips camera from going up to down. If I am keep on going upward or downward it seems that my camera is going up and then down and again up and so on.
Please help…
So, imagine that your back is glued to a large sphere. When you press an ‘up’ button the sphere rotates upwards, after 90 degrees you are now looking down on the top of your cube. If you keep pressing ‘up’, then the sphere rotates further and you are now upside down looking at your cube. This is the behaviour that you have written your script to emulate. Your code looks correct. You might want to think about what camera viewing model you want in your application. What I typically do is have the camera constrained to move around a cylinder. Up/down on the mouse moves up/down the side of the cylinder, and left/right moves around the surface. So basically you just need one variable, height, and a second alpha, which is the angle of rotation around the cylinder (from zero to 360 degrees.) As the mouse moves you have a position on the cylinder, and then you can just lookat
the cube.
If you ever want the viewer to get closer to the cube, you can use the scroll wheel to dolly in and out, which means moving the camera in the direction from the cylinder to the cube.
I am using this script that works well It can zoom with the weel and rotate 360 degrees. My problem is that I parented the camera under my flying character but when I rotate the character the camera ignores the rotation of the parent. Please give us a hand with fixig this script;
[EDITED] so I extended on of the } in on an if statement so that the camera follows the parent as normal. Before the camera was not responding to parenting.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class DragMouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}