- Home /
I'm clueless about how to make my script change its position based on how close it is to the wall..
Problem: After spending several hours on different tutorials(mainly this one https://www.youtube.com/watch?v=MkbovxhwM4I) I have no clue how to execute a script that makes my camera scale depending on how close it is to the wall. if I bound the camera to a empty gameobject with a rigidbody, the camera wouldn't try to go back to its main position. Also, the tutorial has problems like introducing code out of video and has code to make the camera orbit mixed in when all I want is a few fixed positions.
What I want: Simply all I want is my camera to to move closer/farther depending on how close it is to the wall. There is no complicated geometry to account for. I have the 6 fixed camera angles accounted for. I just don't know how to make my camera scale depending on how close it is to the wall :/
What my game's layout looks like
My current script:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
public int count;
private Vector3 Campos;
public Transform player;
public GameObject Player;
public int Distance;
public void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
transform.position = Player.transform.position + Campos;
transform.LookAt(player);
}
public void Update()
{
if (count == 0)
{
Campos = new Vector3(0f, 5f, -15f);
Debug.Log(count);
Player.transform.eulerAngles = new Vector3(0f, 0f, 0f);
}
else
{
if (count == 1)
{
Campos = new Vector3(15f, 5f, 0f);
Debug.Log(count);
Player.transform.eulerAngles = new Vector3(0f, 270f, 0f);
}
else
{
if (count == 2)
{
Campos = new Vector3(0f, 5f, 7f);
Debug.Log(count);
Player.transform.eulerAngles = new Vector3(0f, 180f, 0f);
}
else
{
if (count == 3)
{
Campos = new Vector3(0f, 8f, 15);
Debug.Log(count);
Player.transform.eulerAngles = new Vector3(0f, 90f, 0f);
}
else
{
if (count <= -1)
{
count = 3;
}
else
{
if (count <= 4)
{
count = 0;
}
//the script is going to account for the ceiling position and floor position so don't worry about that
}
}
}
}
}
}
}
I believe the best way would be to use raycasting and adjust based on the distance from the hit.
Ik that I need to use raycast but I don't know how to execute that ;-;
hmmmm. do you need the camera to back up based on the players distance from the wall or the cameras distance from the wall?