After reviewing the comments below, I am editing the question to make it more feasible.
I’m using the PUN demos to re-create my own world. Included with the demo is a “ThirdPersonCamera.cs” script. This script appears to have simple “camera follow player” and “snap camera to behind player” principles. I have disabled the snap to behind script, and would like to add a “mouse driven camera orbit” script to the current script.
void LateUpdate ()
// Update camera position - specifics are delegated to camera mode functions
{
if (
(Input.GetMouseButton (0) || Input.GetMouseButton (1)) && // Act if a mouse button is down
(!requireLock || controlLock || Screen.lockCursor) // ... and we're allowed to
)
{
if (controlLock)
{
Screen.lockCursor = true;
}
FreeUpdate ();
lastStationaryPosition = target.transform.position;
// Update the stationary position so we don't get an immediate snap back when releasing the mouse button
}
else
{
if (controlLock)
{
Screen.lockCursor = false;
}
Vector3 movement = target.transform.position - lastStationaryPosition;
if (new Vector2 (movement.x, movement.z).magnitude > movementThreshold)
// Only update follow camera if we moved sufficiently
{
FollowUpdate ();
}
}
DistanceUpdate ();
}
above is the primary script i would like to add to my current script.
The LateUpdate() function in my current script contains:
void LateUpdate()
{
Apply(transform, Vector3.zero);
}
requireLock, controlLock and Screen.lockCursor don’t appear to exist in my current script. I realize Screen.lockCursor is a global command, but requireLock and controlLock contain many previous calculations that i believe to be irrelevant. However, they are relevant if i want ground boundaries and object collision detection.
Its really got about everything i could ever want my camera to do! I had to make a few adjustments and now i’m ready to queue it to follow always, but only rotate on mousedown!
if anyone wants to take a crack at it while i do some playing around, here’s my edited script:
using UnityEngine;
using System.Collections;
public class ThirdPersonCamera : MonoBehaviour
{
public Transform target;
// The object we're looking at
new public Camera camera;
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;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start () {
camera = Camera.main;
Vector3 angles = camera.transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
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, camera.transform.position, out hit)) {
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
camera.transform.rotation = rotation;
camera.transform.position = position;
Vector3 pHeight = target.position;
pHeight.y = target.position.y+1.5f;
camera.transform.LookAt(pHeight);
}
}
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);
}
}