Hey guys.
So right now I have two separate scripts that work just fine alone. But I’m having an issue combining them.
First one is:
using UnityEngine;
using System.Collections;
public class CameraMouseOrbit : MonoBehaviour
{
public Transform Target;
public float Distance = 5.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
public float x;
public float y;
void Awake()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
if(GetComponent<Rigidbody>() != null)
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate()
{
if(Target != null)
{
x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -Distance)) + Target.position;
transform.rotation = rotation;
transform.position = position;
}
}
private float ClampAngle(float angle, float min, float max)
{
if(angle < -360)
{
angle += 360;
}
if(angle > 360)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
}
And the second one is:
using UnityEngine;
using System.Collections;
public class CameraCollision : MonoBehaviour
{
Transform playerTransform;
Quaternion targetLook;
Vector3 targetMove;
public float rayHitMoveInFront = 0.1f;
Vector3 targetMoveUse;
public float smoothLook = 0.5f;
public float smoothMove = 0.5f;
Vector3 smoothMoveV;
public float distFromPlayer = 5.0f;
public float heightFromPlayer = 2.0f;
// Use this for initialization
void Start ()
{
playerTransform = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void Update ()
{
targetMove = playerTransform.position + (playerTransform.rotation * new Vector3(0, heightFromPlayer, -distFromPlayer));
RaycastHit hit;
if (Physics.Raycast(playerTransform.position, targetMove - playerTransform.position, out hit, Vector3.Distance(playerTransform.position, targetMove)))
{
targetMoveUse = Vector3.Lerp (hit.point, playerTransform.position, rayHitMoveInFront);
}
else
{
targetMoveUse = targetMove;
}
transform.position = Vector3.SmoothDamp(transform.position, targetMoveUse, ref smoothMoveV, smoothMove);
}
}
The first one is the basic mouse orbit script. The second one zooms the camera in if it collides with an object. I know the problem is just because the mouse orbit is constantly moving the camera position so the collision detection doesn’t have time to adjust the camera…
It might just be due to lack of sleep but I can’t figure out how to work around this…
Any help would be appreciated.
Thanks.