Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Transform is sinking into floor

Discussion in 'Getting Started' started by beovnanno, Apr 3, 2024.

  1. beovnanno

    beovnanno

    Joined:
    Jan 20, 2024
    Posts:
    5
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Windows;
    6. using static UnityEditor.PlayerSettings;
    7.  
    8. public class PlayerMovement : MonoBehaviour
    9. {
    10.    
    11.     public float Speed;
    12.  
    13.     bool Grounded;
    14.  
    15.     private void Gravity()
    16.     {
    17.         transform.Translate(0, -.2f, 0);
    18.     }
    19.     private void isGrounded()
    20.     {
    21.         if(Physics.Raycast(transform.position,Vector3.down, 1.3f))
    22.         {
    23.             Grounded = true;
    24.          
    25.         }
    26.         else
    27.         {
    28.             Grounded = false;
    29.            
    30.         }
    31.  
    32.     }
    33.  
    34.    Vector3 floorAngle(Vector3 pos)
    35.     {
    36.         RaycastHit hit;
    37.         if(Physics.Raycast(transform.position, Vector3.down, out hit, 1.3f))
    38.         {
    39.             return Vector3.ProjectOnPlane(pos, hit.transform.up);
    40.         }
    41.         else
    42.         {
    43.             return pos;
    44.         }
    45.     }
    46.  
    47.  
    48.  
    49.     void Update()
    50.     {
    51.         float xInput = UnityEngine.Input.GetAxis("Horizontal");
    52.         float yInput = UnityEngine.Input.GetAxis("Vertical");
    53.  
    54.         Vector3 input = new Vector3(xInput, 0, yInput).normalized;
    55.  
    56.         input = floorAngle(input);
    57.  
    58.         isGrounded();
    59.  
    60.         transform.Translate(input * .02f);
    61.  
    62.         if (Grounded == false)
    63.         {
    64.             Gravity();
    65.         }
    66.         Debug.DrawRay(transform.position, Vector3.down * 1.3f, Color.red);
    67.         transform.Translate(input * Speed);
    68.        
    69.     }
    70. }
    71.  
    UPLOAD_TO_UNITYFORUMS.gif

    I've been working on my player movement, and thanks to this forum I've made some progress on getting more cleaner movement. I was testing how I can get the player up inclined floors and the like, and noticed how on steeper slopes it slowly sinks into the floor. I tried raycasting a slopecheck ahead of it to see if that would work, but it only makes the capsule sinking even faster. I have no idea what to do, can someone help me with a solution?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,085
    You should probably just switch to using a character controller because creating a full controller yourself could prove to be quite difficult. For example, after you've figured out how to handle slopes you are next going to have to handle collisions with walls which I think you'll find is much trickier than ground collisions. And then after walls you'll need to work out how to climb steps. So instead of reinventing the wheel you could use Unity's character controller or a rigidbody.

    But if you still want to create your own controller then you'll need to use CapsuleCast and ProjectOnPlane for deflecting wall collisions.
     
    Last edited: Apr 3, 2024
  3. beovnanno

    beovnanno

    Joined:
    Jan 20, 2024
    Posts:
    5
    You might be right that I should just stick to the tools already made for us. I just wanted to learn how to make my own controller for the sake of curiosity. Any material you recommend for learning this kind of stuff? (Preferably not a youtube video.)
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,085
    You could do a search on GitHub for something like "Unity custom character controller". Also, still look on youtube for tutorials because very often they'll add a link to their code in the video's description.

    And study Unity's character controller by making it do a single move/step forward 10 meters and seeing how it behaves when hitting a wall or slope at an angle. I think from this it should be obvious what's happening behind the scenes and you'll be able to reverse engineer it.
     
    beovnanno likes this.
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,085
    BTW - line 39 in your script should probably be:
    Code (CSharp):
    1. return Vector3.ProjectOnPlane(pos, hit.normal);
    ...and use SphereCast.
     
    Last edited: Apr 4, 2024
  6. beovnanno

    beovnanno

    Joined:
    Jan 20, 2024
    Posts:
    5
    Will do. Thank you