- Home /
 
Camera gets flung off of the map when the player collides with certain objects.
When the player collides with a collider and I move forward a lot, the camera gets flung far from the map and orbits it when I look around. How do I fix this?
Here is a video showing the issue
Here is the player controller Script (I deleted some parts that have nothing to do with movement because it is a huge script)
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI;
 
 
 public class PlayerController : MonoBehaviour
 {
     public float MaxSpeed;
     public int damage = 1;
     public float attackCool;
     public int HP;
     public float vert;
     public float horz;
     public float speed;
     public float lookx;
     public float looky;
     public float minx;
     public float maxx;
     public float sens;
     private float x;
     private float y;
     private float z;
     public int forceConst = 50;
     private Rigidbody body;
     public GameObject cam;
     public bool onGround;
     public Vector3 crosshare;
     public GameObject[] place;
     public int select;
     private LineRenderer line;
     private Text HPdisp;
     public int range;
     public GameObject maxTarget;
     public bool fps;
     public Camera thirdPerson;
     private Camera firstPerson;
     public GameObject preview;
     private bool canPlace;
     private bool canBreak;
     public bool canAttack;
     private RaycastHit delete;
     private RaycastHit create;
     public RaycastHit attack;
     public float minDist;
     public GameObject Weapon;
     private Quaternion weaponOgAngles;
     private GameObject overworld;
     private GameObject underworld;
     // Start is called before the first frame update
     void Start()
     {
     
         body = GetComponent<Rigidbody>();
         line = cam.GetComponent<LineRenderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         
         
             body.velocity = Vector3.ClampMagnitude(body.velocity, MaxSpeed);
         
     
     maxTarget.transform.localPosition = new Vector3(0, 0, range);
         Cursor.lockState = CursorLockMode.Locked;
         //Movement Keys
         vert = Input.GetAxis("Vertical");
         horz = Input.GetAxis("Horizontal");
         x = horz * speed;
         z = vert * speed;
         //
         
               body.AddRelativeForce(x, 0f, z);
         //
         if (Input.GetKeyDown(KeyCode.P) && fps)
         {
             thirdPerson.enabled = true;
             firstPerson.enabled = false;
         }
         if (Input.GetKeyDown(KeyCode.P) & !fps)
         {
             thirdPerson.enabled = false;
             firstPerson.enabled = true;
         }
         //Look around
         lookx += Input.GetAxis("Mouse Y");
         lookx = Mathf.Clamp(lookx, minx, maxx);
         looky += Input.GetAxis("Mouse X");
         cam.transform.eulerAngles = new Vector3(-lookx * sens, looky * sens, 0);
         transform.eulerAngles = new Vector3(0, looky * sens, 0);
         //Jump
         if (Input.GetKeyDown(KeyCode.Space) && onGround == true)
         {
             body.AddForce(0, forceConst, 0, ForceMode.Impulse);
             onGround = false;
         }
         
         //Place Blocks
         
         crosshare = cam.GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, 0));
         
         preview.transform.position = create.point;
         
         canPlace = Physics.Raycast(crosshare, cam.transform.forward, out create, range);
         canBreak = Physics.Raycast(crosshare, cam.transform.forward, out delete, range);
         canAttack = Physics.Raycast(crosshare, cam.transform.forward, out attack, range);
 
         if (create.distance == 0)
         {
             preview.transform.position = maxTarget.transform.position;
         }
         if (create.distance < minDist)
         {
             canPlace = false;
         }
         if (Input.GetMouseButtonDown(1))
         {
             preview.SetActive(true);
             if (canPlace == true)
             {
                 Instantiate(place[select], create.point, cam.transform.rotation);
             }
             if (create.distance == 0)
             {
                 Instantiate(place[select], maxTarget.transform.position, cam.transform.rotation);
             }
            Debug.Log(create.distance);
         }
 
         //Break them
         if (Input.GetMouseButtonDown(0))
         {
             preview.SetActive(true);
             crosshare = cam.GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, 0));
             if (canBreak == true)
             {
                 if (delete.collider.tag == "Blocks")
                 {
                     Destroy(delete.collider);
                 }
             }
         }
         
         
     private void OnCollisionEnter(Collision collider)
     {
         if (collider.collider.tag == "Walkable" || collider.collider.tag == "Blocks")
         {
             onGround = true;
         }
     }
     private void OnCollisionStay(Collision collider)
     {
         if (collider.collider.tag == "Entity" && 
 
 
 
 }
 
 
 
 
              
               Comment
              
 
               
              Your answer