Untitled
unknown
plain_text
2 years ago
1.7 kB
9
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamCollision : MonoBehaviour
{
Transform cam = null;
public Transform referenceTransform;
public float collisionOffset = 0.3f; //To prevent Camera from clipping through Objects
public float cameraSpeed = 15f; //How fast the Camera should snap into position if there are no obstacles
Vector3 defaultPos;
Vector3 directionNormalized;
Transform parentTransform;
float defaultDistance;
// Start is called before the first frame update
void Start()
{
defaultPos = transform.localPosition;
directionNormalized = defaultPos.normalized;
parentTransform = transform.parent;
defaultDistance = Vector3.Distance(defaultPos, Vector3.zero);
//Lock cursor
cam.transform.rotation = Quaternion.Euler(-180, -180, 0);
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
cam.transform.rotation = Quaternion.Euler(-180, -180, 0);
}
// LateUpdate is called after Update
void LateUpdate()
{
Vector3 currentPos = defaultPos;
RaycastHit hit;
Vector3 dirTmp = parentTransform.TransformPoint(defaultPos) - referenceTransform.position;
if (Physics.SphereCast(referenceTransform.position, collisionOffset, dirTmp, out hit, defaultDistance)) {
currentPos = (directionNormalized * (hit.distance - collisionOffset));
transform.localPosition = currentPos;
} else {
transform.localPosition = Vector3.Lerp(transform.localPosition, currentPos, Time.deltaTime * cameraSpeed);
}
}
}
Editor is loading...
Leave a Comment