Mouse Orbit Script Help
First off I'm not a coder or scripter by any means and i cannot imagine why something this basic isnt built in.
So, I have the Mouse Orbit script working. While playing I can freely move the mouse to rotate the camera. The script is attached to the camera. However there are a few pretty basic things I want to do but simply cannot figure out.
When I finally get this code working I will freely upload it to the asset store for anyone to use. Cause this is basic 3rd person game shit.
I want the mouse orbit cam to only run while I hold Left Click
When I release Left Click, I want the camera to stay where it was until there is a movement input, then the camera would re-align behind the character.
I want to offset the camera slightly to the right of my target
Here is the current code I found on the internet:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yOffset = 1;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
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 + new Vector3(0, yOffset, 0);
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);
}
}
"
Your answer
![](https://koobas.hobune.stream/wayback/20220612122809im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
my script was written but does not want to work 1 Answer
Is there any way to increase score with UI when I destroy an object? 0 Answers
How do I assign values to variables in the Update function only ONCE? 1 Answer
Toggle direction 0 Answers
level manger and starting scene will not work together 0 Answers