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. Dismiss Notice

Question Movement through floor

Discussion in 'Scripting' started by thornilsen33, Jun 30, 2023.

  1. thornilsen33

    thornilsen33

    Joined:
    Jun 17, 2023
    Posts:
    1
    Hi, I have been trying to write my own first person movement that follows the first person camera view.

    This works somewhat. The problem is when I am looking down, the "player" moves through the floor, and if I am looking up the player is moving away from the ground.

    Also have another question regarding my lookAround script. I can not for the life of me limit the vertical rotation to -90 and 90 degrees. Have tried using mathf.clamp and using the return float here like this
    playerCamera.transform.Rotate(-clampedMouseY, 0, 0, Space.Self);
    , but it still lets me rotate freely. Is there a better way to get the correct vertical angle on the playerCamera rigidbody?

    Any help would be highly appreciated.
    Have been struggling with these issues for a couple of hours now :S

    Code (CSharp):
    1.      public Rigidbody player;
    2.     public Rigidbody playerCamera;
    3.  
    4.     public float moveSpeed;
    5.  
    6. void moveRelativeToCamera()
    7.     {
    8.         float forwardMovement = Input.GetAxis("Vertical");
    9.         float sideMovement = Input.GetAxis("Horizontal");
    10.         //Directional movement:
    11.         if (forwardMovement == 1) //Forward relative to camera
    12.         {
    13.             player.position.x = player.position + playerCamera.transform.forward * moveSpeed * Time.deltaTime;
    14.         }
    15.         else if (forwardMovement == -1) //Back relative to camera
    16.         {
    17.             player.position = player.position + -playerCamera.transform.forward * moveSpeed * Time.deltaTime;
    18.         }
    19.         else if (sideMovement == 1) //Right relative to camera
    20.         {
    21.             player.position = player.position + playerCamera.transform.right * moveSpeed * Time.deltaTime;
    22.         }
    23.         else if (sideMovement == -1) //Left relative to camera
    24.         {
    25.             player.position = player.position + -playerCamera.transform.right * moveSpeed * Time.deltaTime;
    26.         }
    27. }
    28.  
    29. void lookAround()
    30.     {
    31.        float mouseX = Input.GetAxis("Mouse X") * mouseSens / Time.deltaTime; //Horizontal input
    32.        float mouseY = Input.GetAxis("Mouse Y") * mouseSens / Time.deltaTime; //Vertical input
    33.        playerCamera.transform.Rotate(-mouseY, 0, 0, Space.Self); //Rotates vertically
    34.        player.transform.Rotate(0, mouseX, 0, Space.World); //Rotates horizontally
    35.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    Ryiah likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    When moving forwards and backwards you should use player.transform.forward and not playerCamera.transform.forward.

    For clamping the camera you need to set the camera direction using euler angles so then you can clamp them.

    Your script is quite bad and so I suggest you use Google to search for another script to work with. There's many FPS camera control scripts out there.