Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Camera moves through terrain

Discussion in 'Editor & General Support' started by AngusWP, Apr 4, 2020.

  1. AngusWP

    AngusWP

    Joined:
    Mar 16, 2020
    Posts:
    11
    Hi. I am having some issues getting my camera script to stop going through the terrain. This is the original issue I had - https://prnt.sc/rsvjao

    I made a fix, but it only partially fixed the issue, as now if the player runs down a hill (a place with a descending y coordinate) with the camera along the floor, the camera passes through the terrain that is above the camera. https://prnt.sc/rsvisb
    Here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour {
    6.  
    7.     public Transform playerCamera, character, centrePoint;
    8.     public float rotationSpeed, zoomSpeed, playerSpeed, minZoom, maxZoom, increaseMouseY, defaultZoom, sprintSpeed, yCameraOffset;
    9.  
    10.     private float mouseX, mouseY, moveFB, moveLR;
    11.     private float zoom;
    12.     // move front back, move left right
    13.  
    14.     public Animator anim;
    15.  
    16.     void Start() {
    17.         zoom = defaultZoom; // base zoom  value
    18.     }
    19.  
    20.     void Update() {
    21.  
    22.         zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
    23.  
    24.         if (zoom > minZoom) {
    25.             zoom = minZoom;
    26.         }
    27.  
    28.         if (zoom < maxZoom) {
    29.             zoom = maxZoom;
    30.         }
    31.  
    32.         playerCamera.transform.localPosition = new Vector3(0, yCameraOffset, zoom);
    33.         // this handles the actual zoom
    34.  
    35.         if (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
    36.             anim.SetBool("running", true);
    37.         }
    38.  
    39.         if (Input.GetKeyUp(KeyCode.LeftShift)) {
    40.             anim.SetBool("running", false);
    41.         }
    42.  
    43.         float realSpeed = (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) ? sprintSpeed : playerSpeed;
    44.  
    45.  
    46.         if (Input.GetMouseButton(1)) {
    47.             mouseX -= Input.GetAxis("Mouse Y") * rotationSpeed; // dont question this inversion, it works... this works for me at the moment but probably some F*** up in settings
    48.             mouseY += Input.GetAxis("Mouse X") * rotationSpeed;
    49.         }
    50.  
    51.  
    52.  
    53.         mouseX = Mathf.Clamp(mouseX, Terrain.activeTerrain.GetPosition().x - 40, 25);
    54.         // THIS IS THE LINE WHERE I TRIED TO FIX IT ^^^
    55.  
    56.         playerCamera.LookAt(centrePoint);
    57.         centrePoint.localRotation = Quaternion.Euler(mouseX, mouseY, -1);
    58.  
    59.         moveLR = Input.GetAxis("Horizontal") * realSpeed;
    60.         moveFB = Input.GetAxis("Vertical") * realSpeed;
    61.  
    62.         if (moveFB != 0 || moveLR != 0) {
    63.             anim.SetBool("walking", true);
    64.         } else {
    65.             anim.SetBool("walking", false);
    66.         }
    67.  
    68.         Vector3 movement = new Vector3(moveLR, 0, moveFB);
    69.         movement = character.rotation * movement;
    70.  
    71.         character.GetComponent<CharacterController>().Move(movement * Time.deltaTime);
    72.         centrePoint.position = new Vector3(character.position.x, character.position.y + increaseMouseY, character.position.z);
    73.  
    74.         if (Input.GetAxis("Vertical") != 0) {
    75.             Quaternion turnAngle = Quaternion.Euler(0, centrePoint.eulerAngles.y, 0);
    76.             character.rotation = Quaternion.Slerp(character.rotation, turnAngle, rotationSpeed * Time.deltaTime);
    77.         }
    78.     }
    79.  
    80. }
    81.  
     
  2. AngusWP

    AngusWP

    Joined:
    Mar 16, 2020
    Posts:
    11
  3. AngusWP

    AngusWP

    Joined:
    Mar 16, 2020
    Posts:
    11
  4. AngusWP

    AngusWP

    Joined:
    Mar 16, 2020
    Posts:
    11