Search Unity

Help Wanted How to make a Player can walk on Slopes? | 3d / Rigidbody Controller

Discussion in 'Scripting' started by unity_FUc_6LWWe7c3mw, Sep 19, 2021.

  1. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    42
    Good day,
    i make my own 3d game momently i a at the 3d Character and i want the character to can Walk on Slopes i searched in the webs usw. and i found many thinks but nothing worked. What can i do?
    (For Code see down bellow).
    I hope someone can help me,
    FyMa2618.

    Code (CSharp):
    1. //Player Movement
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     //GRAIVTY IN EINSTELLUNGEN = -20f;
    9.     private Vector3 PlayerMovementInput;
    10.     private Vector2 PlayerMouseInput;
    11.     CapsuleCollider collider;
    12.     float originalHeight;
    13.     private float xRot;
    14.  
    15.     public WallRun wallRun;
    16.     public LayerMask FloorMask;
    17.     public Transform playerCamera;
    18.     public Transform playerFeet;
    19.     public Transform orientation;
    20.     public Rigidbody playerBody;
    21.     [Space]
    22.     public float Speed = 15;
    23.     public float SlideSpeed = 10f;
    24.     public float sensitivity = 3f;
    25.     public float JumpForce = 15f;
    26.     float reducedHeight = 1.1f;
    27.  
    28.     void Start()
    29.     {
    30.         collider = GetComponent<CapsuleCollider>();
    31.         originalHeight = collider.height;
    32.         Cursor.lockState = CursorLockMode.Locked;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         PlayerMovementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    38.         PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    39.  
    40.         MovePlayer();
    41.         MovePlayerCamera();
    42.     }
    43.  
    44.     private void MovePlayer()
    45.     {
    46.         Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput) * Speed;
    47.         playerBody.velocity = new Vector3(MoveVector.x, playerBody.velocity.y, MoveVector.z);
    48.  
    49.         if (Input.GetKeyDown(KeyCode.Space))
    50.         {
    51.             if(Physics.CheckSphere(playerFeet.position, 0.1f, FloorMask))
    52.             {
    53.                 playerBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
    54.             }
    55.         }
    56.         if (Input.GetKeyDown(KeyCode.C))
    57.             Sliding();
    58.         else if (Input.GetKeyUp(KeyCode.C))
    59.             GoUp();
    60.     }
    61.     private void MovePlayerCamera()
    62.     {
    63.         xRot -= PlayerMouseInput.y * sensitivity;
    64.  
    65.         transform.Rotate(0f, PlayerMouseInput.x * sensitivity, wallRun.tilt);
    66.         playerCamera.transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
    67.     }
    68.  
    69.     private void Sliding()
    70.     {
    71.         collider.height = reducedHeight;
    72.         playerBody.AddForce(transform.forward * SlideSpeed, ForceMode.VelocityChange);
    73.     }
    74.  
    75.     private void GoUp()
    76.     {
    77.         collider.height = originalHeight;
    78.     }
    79.  
    80. }
    81.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    19,279
  3. unity_FUc_6LWWe7c3mw

    unity_FUc_6LWWe7c3mw

    Joined:
    Jan 1, 2021
    Posts:
    42
    Good day,
    thanks for response but i want to use my own charactercontroller-Script but i dont found how to let the Player walk on Slopes and thats my Question.
     
unityunity