So I’m having trouble with adjusting my camera distance when hitting or hiding behind walls. The logic for this didn’t seem to crazy in my head when I started, but at this point I keep having one issue or another and am wondering what I’m doing wrong.
My first thought was very simple, as I had done this long long ago in a previous project. I however no long have that project to reference off, but hey, how hard could it be right? So I started with a raycast. Not I realize that when doing a raycast for this I’m trying to see if there’s anything between the player and the camera. Wording is key, meaning I would have to start from the player and send the ray towards the camera’s current position. No biggy. Or at least I thought so. I’ve noticed that I get some seriously odd hits when sending a raycast out from said player to camera. Here is my code, I’ll break down the ending section that I was trying after.
using UnityEngine;
public class cameraControls : MonoBehaviour {
//player model that we gravitate around
[SerializeField]
private Transform Player;
//the left/right or X direction the camera is facing
private float heading = 0;
//the up/down or Y direction the camera is facing
private float tilt = 20;
//the distance the camera will hover at
private float camDist = 10;
//not the player's model actual height, but the cameras height aka the player
private float playerHeight = 2;
void Update ()
{
}
void LateUpdate ()
{
//get our X heading
heading += Input.GetAxis("Mouse X") * Time.deltaTime * 180;
//get our Y tilt
tilt += Input.GetAxis("Mouse Y") * Time.deltaTime * -180;
//clamping the tilt so the player's camera doesn't go too far on Y rotation
tilt = Mathf.Clamp(tilt, -45, 45);
//set the rotation of the camera
transform.rotation = Quaternion.Euler(tilt, heading, 0);
//set the position of the camera
transform.position = Player.position - transform.forward * camDist + Vector3.up * playerHeight;
//do our camera check to make sure nothings between the player and the camera
//adjust the camera if there is something blocking the view
DoCameraCheck();
}
//Camera check to make sure nothing's between the camera and us
void DoCameraCheck()
{
RaycastHit objectHit;
//our hard to make raycast
if(Physics.Raycast(Player.position, transform.position, out objectHit))
{
camDist = objectHit.distance;
//camDist = Mathf.Clamp(camDist, 4, 10);
}
else
{
camDist = 10;
}
Debug.DrawRay(Player.position, transform.position, Color.yellow);
}
}
So what I did was create the raycast and a hit, if the ray hit anything it would adjust the camDist to that distance. I even played with clamping to ensure it didn’t go over or under a certain amount. And if it’s not hitting anything, reset back to default 10. The problem is as I kept playing around with this it worked decent on the wall, but was hitting nothing above, crazy on the floor with a flickering effect, and placement seemed off. I even switched between linecast and raycast and honestly had better luck with that.
I then tried drawing a ray between the 2 and it looks fine in that case until I started trying to move things. Long story short, I’ve gained no ground the last few days and have only obtained headaches trying to do what I thought would be a simple concept.