Search Unity

Question Floor Collision Question

Discussion in 'Physics' started by Scarecrow71, Nov 9, 2023.

  1. Scarecrow71

    Scarecrow71

    Joined:
    Nov 9, 2023
    Posts:
    2
    I am just getting started with game development in Unity, and I'm going through the book "Unity In Action 3rd Edition". The book itself is great; it's helping me get acquainted with the Unity Editor, and teaching me some programming concepts I'm not already familiar with. But I'm running into an issue, and I'm not sure how to correct this. Hence, the forum post.

    In the book, we create a player object and attach several scripts to it for input (mouse and keyboard). The object itself has a transform scale of 1, 1, 1 (x, y, z), and the Character Controller component has a height of 2. The scripts that control movement are fairly simple:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6.  
    7. public class MouseLook : MonoBehaviour
    8. {
    9.     public enum rotationAxes
    10.     {
    11.         MouseXAndY = 0,
    12.         MouseX = 1,
    13.         MouseY = 2
    14.     }
    15.  
    16.     public rotationAxes axes = rotationAxes.MouseXAndY;
    17.    
    18.     public float sensitivityHor = 9.0f;
    19.     public float sensitivityVert = 9.0f;
    20.  
    21.     public float minimumVert = -45.0f;
    22.     public float maximumVert = 45.0f;
    23.  
    24.     private float verticalRot = 0;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         Rigidbody body = GetComponent<Rigidbody>();
    30.         if (body != null)
    31.         {
    32.             body.freezeRotation = true;
    33.         }
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (axes == rotationAxes.MouseX)
    40.         {
    41.             //Horizontal rotation code
    42.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
    43.         }
    44.         else if (axes == rotationAxes.MouseY)
    45.         {
    46.             //Vertical rotation code
    47.             verticalRot -= Input.GetAxis("Mouse Y") * sensitivityVert;
    48.             verticalRot = Mathf.Clamp(verticalRot, minimumVert, maximumVert);
    49.  
    50.             float horizontalRot = transform.localEulerAngles.y;
    51.             transform.localEulerAngles = new Vector3(verticalRot, horizontalRot, 0);
    52.         }
    53.         else
    54.         {
    55.             //Horizontal and vertical rotation code
    56.             verticalRot -= Input.GetAxis("Mouse Y") * sensitivityVert;
    57.             verticalRot = Mathf.Clamp(verticalRot, minimumVert, maximumVert);
    58.  
    59.             float delta = Input.GetAxis("Mouse X") * sensitivityHor;
    60.             float horizontalRot = transform.localEulerAngles.y + delta;
    61.  
    62.             transform.localEulerAngles = new Vector3(verticalRot, horizontalRot, 0);
    63.         }
    64.     }
    65. }
    66.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Security.Cryptography;
    4. using System.Threading;
    5. using UnityEngine;
    6.  
    7. [RequireComponent(typeof(CharacterController))]
    8. [AddComponentMenu("Control Script/FPS Input")]
    9.  
    10. public class FPSInput : MonoBehaviour
    11. {
    12.     public float speed = 12.0f;
    13.     public float gravity = -9.8f;
    14.  
    15.     private CharacterController charController;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         charController = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         float deltaX = Input.GetAxis("Horizontal") * speed;
    27.         float deltaZ = Input.GetAxis("Vertical") * speed;
    28.         Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    29.  
    30.         movement = Vector3.ClampMagnitude(movement, speed);
    31.         movement.y = gravity;
    32.         movement *= Time.deltaTime;
    33.         movement = transform.TransformDirection(movement);
    34.  
    35.         charController.Move(movement);
    36.     }
    37. }
    38.  

    As an aside, the Axis on the mouse script is set to Mouse X, and there is a second instance of this script attached to the main camera that is positioned on top of the player object to allow for Mouse Y.

    This all works just fine, and I can use mouse input to look around and keyboard input to move. And I stay on the floor, with gravity keeping me there and not allowing me to fly around.

    I want to alter the size of the player object so it is in line with the scale of the room it is in. The room has an outer size of 50 x 50, with a wall height of 10. If that's feet, the scale of the player should be 6 (to keep things lined up), with a total diameter of 6 (3 in all directions). However, when I change any of the values in the Unity Editor (Player Transform Scale x/y/z, Character Controller Height/Radius), the player object immediately falls through the floor at runtime. Setting these values back to the original values I stated earlier prevents this, but any change in them and the object falls through the floor.

    So what is it that I am doing wrong here? What other value/parameter needs to be set so that the object can be sized appropriately for the scene, but so that it doesn't fall through the solid object it is supposed to be standing on? Does the object need to have its values set before attaching a script to it?
     
  2. Scarecrow71

    Scarecrow71

    Joined:
    Nov 9, 2023
    Posts:
    2
    And I derped on this. Needed to move the object along the y axis relative to the change in size.