Hello Unity Community. I’ve been banging my head on this issue for whole day now, and I’d like to reach out to the community for help. Some background information: I am making a D3-esque game with the Camera following the Player I have set up. Now Additionally, I would like to have the camera rotate around the character on the x axis. This is proving to be difficult. I’ve messed around with the different scripts from the web and from the Wiki, but to not avail, I still havent figured it out. Here is the code I am using below. Some insight into this would be greatly appreciated.
//This is my Camera Follow Script
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
//This is the Mouse Orbit Script I am using
using UnityEngine;
using System.Collections;
[AddComponentMenu("Infinite Camera-Control/Mouse Orbit with zoom")]
public class OrbitOnMouse : MonoBehaviour {
public Transform target;
public float xSpeed = 12.0f;
public float distance ;
public Vector3 position;
public bool isActivated;
float x = 0.0f;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
}
void LateUpdate () {
// only update if the mousebutton is held down
if (Input.GetMouseButtonDown(1)){
isActivated = true;
}
// if mouse button is let UP then stop rotating camera
if (Input.GetMouseButtonUp(1))
{
isActivated = false;
}
if (target && isActivated) {
// get the distance the mouse moved in the respective direction
x = Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
// when mouse moves left and right we actually rotate around local y axis
transform.RotateAround(target.position,transform.up, x);
// reset back to 0 so it doesn't continue to rotate while holding the button
x=0;
}
}
}
is target nil?
I don’t see it being assigned anything.
Look for a script called OrbitCam… In standard assets I think… Not sure… But if you can’t find it lemme know and I’ll post out later when I get home.
Found it! Here ya go…
#pragma strict
public var TargetLookAt : Transform;
public var Distance : float = 5.0;
public var DistanceMin : float = 3.0;
public var DistanceMax : float = 10.0;
private var mouseX : float = 0.0;
private var mouseY : float = 0.0;
private var startingDistance : float = 0.0;
private var desiredDistance : float = 0.0;
public var X_MouseSensitivity : float = 5.0;
public var Y_MouseSensitivity : float = 5.0;
public var MouseWheelSensitivity : float = 5.0;
public var Y_MinLimit : float = -40.0;
public var Y_MaxLimit : float = 80.0;
public var DistanceSmooth : float = 0.05;
private var velocityDistance : float = 0.0;
private var desiredPosition : Vector3 = Vector3.zero;
public var X_Smooth : float = 0.05;
public var Y_Smooth : float = 0.1;
private var velX : float = 0.0;
private var velY : float = 0.0;
private var velZ : float = 0.0;
private var position : Vector3 = Vector3.zero;
function Start()
{
Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
startingDistance = Distance;
Reset();
}
function LateUpdate()
{
if (TargetLookAt == null)
return;
HandlePlayerInput();
CalculateDesiredPosition();
UpdatePosition();
}
function HandlePlayerInput()
{
var deadZone = 0.01; // mousewheel deadZone
if (Input.GetMouseButton(1))
{
mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
}
// this is where the mouseY is limited - Helper script
mouseY = Mathf.Clamp(mouseY, Y_MinLimit, Y_MaxLimit);
// get Mouse Wheel Input
if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
{
desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity), DistanceMin, DistanceMax);
}
}
function CalculateDesiredPosition()
{
// Evaluate distance
Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth);
// Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
function CalculatePosition(rotationX : float, rotationY : float, distance : float)
{
var direction : Vector3 = Vector3(0, 0, -distance);
var rotation : Quaternion = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + (rotation * direction);
}
function UpdatePosition()
{
var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth);
var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth);
var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth);
position = Vector3(posX, posY, posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
function Reset()
{
mouseX = 0;
mouseY = 10;
Distance = startingDistance;
desiredDistance = Distance;
}
function ClampAngle(angle : float, min : float, max : float)
{
while (angle < -360 || angle > 360)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
}
}