- Home /
Need help with a mouse orbit script
Hi! i need some help with a camera issue. Im making a 3rd person game and i want a drag camera orbit thingy. I have tried MouseOrbitImproved (Author Veli V). but it has a mouse bug. then i tried this camera orbit on mouse drag(forum), but it has a smoothing thingy and it is very annoying. so i made like a mix of these two and i got good result but i am not really satisfied by that the camera "bouncing" back when you get to a distance, i want the camera to have a maxdistance, and when it reach the maxdistance it will stay on it, and not "bounce" back.
(Sorry for my grammatical mistakes)
Here is the mix:
using UnityEngine;
using System.Collections;
public class MouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float x = 0.0f;
public float y = 0.0f;
// Use this for initialization
void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if(rigidbody)
{
rigidbody.freezeRotation = true;
}
}
// Update is called once per frame
void Update ()
{
if(target)
{
if(Input.GetMouseButton(0))
{
x += xSpeed * Input.GetAxis("Mouse X");
y += ySpeed * Input.GetAxis("Mouse Y");
}
}
Quaternion rotation = Quaternion.Euler (y, x, 0);
//Mouse ScrollWheel
distance = Mathf.Clamp (distance - Input.GetAxis ("Mouse ScrollWheel") * 5, .5f, 15f);
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;
}
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);
}
}
i don't know exactly what you want, but in the 3rd person game i made, i used a script something like
Camnormal * transform.forward + Playerpos
camnormal would be the normal distance to the player, if you want zoo$$anonymous$$g you can change it based on that, you could make an empty gameobject following the player and rotating with the mouse for transform.forward and playerpos is the transform.position of the player
Answer by deadshot · Oct 22, 2014 at 05:39 AM
using UnityEngine; using UnityEngine; using System.Collections;
//[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")] public class rotateonmouse : 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(1))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * 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;
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);
}
}
for smooth movement , having a max/min disance and allowing to set the rotation speed
Your answer
Follow this Question
Related Questions
Mouse/Wacom stylus drag not working. 1 Answer
camera orbit on mouse drag 2 Answers
Smooth transition of camera targets 2 Answers