Search Unity

Question how to prevent player from moving up slopes too steep?

Discussion in 'Scripting' started by SpyderManToo, Jun 28, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    so in my fps game, im trying to prevent the character from climbing walls/slopes too steep for the player.

    i first tried making a raycast that shoots downward so it gets the normal of the slope and compared to vector3.up, if it is greater than x angle, then you cant move until you slide back down but it is rly buggy and it didnt work

    the second solution was to replace my current movement system, rigidbody.velocity, with a character controller but everything got rly buggy and the other things in my scene were glitching out.

    then i tried to use oncontrollercolliderhit and compare the normal of the hit but it was never called and im not sure when it gets called.

    i cant think of any other solutions and its been bugging me for the past 2 days.

    how can i prevent my player from climbing up slopes too steep?

    i dont want to use charactercontroller

    here is the script
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public static PlayerController instance;
    7.  
    8.     [Header("Movement and Jump")]
    9.     public float moveSpeed;
    10.     public float runSpeed = 12f;
    11.     public float jumpPower;
    12.  
    13.     [HideInInspector]
    14.     public bool canJump;
    15.     private bool canDoubleJump = false;
    16.  
    17.     public Animator anim;
    18.     private Rigidbody rb;
    19.  
    20.     private Vector3 newMovePos;
    21.  
    22.     private float bounceAmount;
    23.     private bool bounce;
    24.     [HideInInspector]
    25.     public bool canWalk;
    26.  
    27.     bool isGrounded;
    28.     public Transform groundCheck;
    29.     public LayerMask ground;
    30.     public float groundDistance = 0.4f;
    31.  
    32.     [Header("Looking")]
    33.     public Transform camHolder;
    34.     public Transform camTrans;
    35.     public float mouseSens;
    36.     float verticalLookRotation = 0f;
    37.  
    38.     [Header("Shooting")]
    39.     public List<Gun> allGuns = new List<Gun>();
    40.     public List<Gun> unlockableGuns = new List<Gun>();
    41.     public Gun activeGun;
    42.     public int startGun;
    43.     private int currentGun;
    44.  
    45.     public LayerMask layersThatAffectShooting;
    46.  
    47.     [Space(25)]
    48.     public Transform firePoint;
    49.  
    50.     [Space(25)]
    51.     public Transform ADSPoint, gunHolder;
    52.     private Vector3 gunStartPos;
    53.     public float adsSpeed = 2f;
    54.  
    55.     [Space(20)]
    56.     public GameObject muzzleFlash;
    57.  
    58.     [Header("Audio")]
    59.     public AudioSource footstepFast, footstepSlow;
    60.  
    61.     private void Awake()
    62.     {
    63.         Time.timeScale = 1f;
    64.         instance = this;
    65.     }
    66.  
    67.     // Start is called before the first frame update
    68.     void Start()
    69.     {
    70.         rb = GetComponent<Rigidbody>();
    71.  
    72.         for (int i = 0; i < allGuns.Count; i++)
    73.         {
    74.             allGuns[i].gameObject.SetActive(false);
    75.         }
    76.  
    77.         currentGun = startGun;
    78.  
    79.         SwitchGun();
    80.  
    81.         gunStartPos = gunHolder.localPosition;
    82.     }
    83.  
    84.     // Update is called once per frame
    85.     void Update()
    86.     {
    87.         if (!UIController.instance.pauseMenu.activeInHierarchy && !GameManager.instance.levelEnding)
    88.         {
    89.             muzzleFlash.SetActive(false);
    90.  
    91.             isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ground);
    92.             Debug.Log(isGrounded.ToString());
    93.  
    94.             muzzleFlash.SetActive(false);
    95.  
    96.             canJump = isGrounded;
    97.  
    98.             Jump();
    99.             HandleADS();
    100.             Look();
    101.             Shoot();
    102.             WeaponSwitchInput();
    103.             SwitchGun();
    104.  
    105.             if(bounce)
    106.             {
    107.                 bounce = false;
    108.                 rb.AddForce(transform.up * bounceAmount, ForceMode.Impulse);
    109.  
    110.                 canDoubleJump = true;
    111.             }
    112.  
    113.             anim.SetFloat("moveSpeed", newMovePos.magnitude);
    114.             anim.SetBool("onGround", canJump);
    115.         }
    116.     }
    117.  
    118.     private void FixedUpdate()
    119.     {
    120.         if (!UIController.instance.pauseMenu.activeInHierarchy && !GameManager.instance.levelEnding)
    121.         {
    122.             Movement();
    123.         }
    124.     }
    125.  
    126.     void Movement()
    127.     {
    128.         float x = Input.GetAxis("Horizontal");
    129.         float y = Input.GetAxis("Vertical");
    130.  
    131.         if (Input.GetKey(KeyCode.LeftShift))
    132.         {
    133.             x *= runSpeed;
    134.             y *= runSpeed;
    135.         }
    136.         else
    137.         {
    138.             x *= moveSpeed;
    139.             y *= moveSpeed;
    140.         }
    141.  
    142.         Vector3 movePos = transform.right * x + transform.forward * y;
    143.         newMovePos = new Vector3(movePos.x, rb.velocity.y, movePos.z);
    144.  
    145.  
    146.  
    147.         rb.velocity = newMovePos;
    148.     }
    149.  
    150.     void Jump()
    151.     {
    152.         //Handle Jumping
    153.         if (Input.GetKeyDown(KeyCode.Space))
    154.         {
    155.             if (canJump && isGrounded)
    156.             {
    157.                 rb.AddForce(transform.up * jumpPower, ForceMode.Impulse);
    158.  
    159.                 //moveInput.y = jumpPower;
    160.  
    161.                 canJump = false;
    162.                 canDoubleJump = true;
    163.  
    164.                 AudioManager.instance.PlaySFX(8);
    165.                
    166.             }
    167.             else if (canDoubleJump)
    168.             {
    169.                 rb.AddForce(transform.up * jumpPower, ForceMode.Impulse);
    170.                 //moveInput.y = jumpPower;
    171.  
    172.                 canDoubleJump = false;
    173.  
    174.                 AudioManager.instance.PlaySFX(8);
    175.             }
    176.         }
    177.     }
    178.  
    179.     void Look()
    180.     {
    181.         transform.Rotate(Vector3.up * mouseSens * Input.GetAxisRaw("Mouse X"));
    182.         verticalLookRotation -= Input.GetAxisRaw("Mouse Y") * mouseSens;
    183.         verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
    184.        
    185.         camHolder.localEulerAngles = new Vector3(verticalLookRotation, 0, 0);
    186.     }
    187.  
    188.     void Shoot()
    189.     {
    190.         switch(activeGun.fireType)
    191.         {
    192.             case FireType.SingleShot:
    193.                 {
    194.                     if (Input.GetMouseButtonDown(0) && activeGun.fireCounter <= 0)
    195.                     {
    196.                         RaycastHit hit;
    197.                         if (Physics.Raycast(camTrans.position, camTrans.forward, out hit, 50f, layersThatAffectShooting))
    198.                         {
    199.                             if (Vector3.Distance(camTrans.position, hit.point) > 2f)
    200.                             {
    201.                                 firePoint.LookAt(hit.point);
    202.                                 //Debug.Log("Fire point is looking at center");
    203.                             }
    204.                         }
    205.                         else
    206.                         {
    207.                             firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
    208.  
    209.                         }
    210.  
    211.                         FireShot();
    212.                     }
    213.                 }
    214.                 break;
    215.  
    216.             case FireType.AutoFire:
    217.                 {
    218.                     if(Input.GetMouseButton(0) && activeGun.fireCounter <= 0)
    219.                     {
    220.                         RaycastHit hit;
    221.                         if (Physics.Raycast(camTrans.position, camTrans.forward, out hit, 50f, layersThatAffectShooting))
    222.                         {
    223.                             if (Vector3.Distance(camTrans.position, hit.point) > 2f)
    224.                             {
    225.                                 firePoint.LookAt(hit.point);
    226.                                 //Debug.Log("Fire point is looking at center");
    227.                             }
    228.                         }
    229.                         else
    230.                         {
    231.                             firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
    232.  
    233.                         }
    234.  
    235.                         if (activeGun.fireCounter <= 0)
    236.                         {
    237.                             FireShot();
    238.                         }
    239.                     }
    240.                     break;
    241.                 }
    242.         }
    243.  
    244.        
    245.     }
    246.  
    247.     public void FireShot()
    248.     {
    249.         if (activeGun.currentAmmo > 0)
    250.         {
    251.  
    252.             activeGun.currentAmmo--;
    253.  
    254.             Instantiate(activeGun.bullet, firePoint.position, firePoint.rotation);
    255.  
    256.             activeGun.fireCounter = activeGun.fireRate;
    257.  
    258.             UIController.instance.ammoText.text = "AMMO: " + activeGun.currentAmmo;
    259.  
    260.             muzzleFlash.SetActive(true);
    261.         }
    262.     }
    263.  
    264.     private void WeaponSwitchInput()
    265.     {
    266.         int previousSelectedWeapon = currentGun;
    267.  
    268.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    269.         {
    270.             if (currentGun >= allGuns.Count - 1)
    271.                 currentGun = 0;
    272.             else
    273.                 currentGun++;
    274.         }
    275.  
    276.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    277.         {
    278.             if (currentGun <= 0)
    279.                 currentGun = allGuns.Count - 1;
    280.             else
    281.                 currentGun--;
    282.         }
    283.  
    284.         if(Input.GetKeyDown(KeyCode.Alpha1))
    285.         {
    286.             currentGun = 0;
    287.         }
    288.  
    289.         if (Input.GetKeyDown(KeyCode.Alpha2) && allGuns.Count >= 2)
    290.         {
    291.             currentGun = 1;
    292.         }
    293.  
    294.         if (Input.GetKeyDown(KeyCode.Alpha3) && allGuns.Count >= 3)
    295.         {
    296.             currentGun = 2;
    297.         }
    298.  
    299.         if (Input.GetKeyDown(KeyCode.Alpha4) && allGuns.Count >= 4)
    300.         {
    301.             currentGun = 3;
    302.         }
    303.         if (previousSelectedWeapon != currentGun)
    304.             SwitchGun();
    305.     }
    306.  
    307.     private void SwitchGun()
    308.     {
    309.         int i = 0;
    310.         foreach(Gun weapon in allGuns)
    311.         {
    312.             weapon.gameObject.SetActive(i == currentGun);
    313.             i++;
    314.         }
    315.  
    316.         activeGun = allGuns[currentGun];
    317.         UIController.instance.ammoText.text = "AMMO: " + activeGun.currentAmmo;
    318.  
    319.         firePoint.position = activeGun.firePoint.position;
    320.     }
    321.  
    322.     public void AddGun(string gunToAdd)
    323.     {
    324.         bool gunUnlocked = false;
    325.  
    326.         if (unlockableGuns.Count > 0)
    327.         {
    328.             for (int i = 0; i < unlockableGuns.Count; i++)
    329.             {
    330.                 if (unlockableGuns[i].gunName == gunToAdd)
    331.                 {
    332.                     gunUnlocked = true;
    333.  
    334.                     allGuns.Add(unlockableGuns[i]);
    335.  
    336.                     unlockableGuns.RemoveAt(i);
    337.  
    338.                     i = unlockableGuns.Count;
    339.  
    340.                     Debug.Log("Adding " + gunToAdd);
    341.                 }
    342.             }
    343.         }
    344.  
    345.         if (gunUnlocked)
    346.         {
    347.             currentGun = allGuns.Count - 1;
    348.  
    349.         }
    350.     }
    351.  
    352.     void HandleADS()
    353.     {
    354.         if(Input.GetMouseButton(1))
    355.         {
    356.             CameraController.instance.ZoomIn(activeGun.zoomAmount);
    357.             gunHolder.position = Vector3.MoveTowards(gunHolder.position, ADSPoint.position, adsSpeed * Time.deltaTime);
    358.         }
    359.         else
    360.         {
    361.             gunHolder.localPosition = Vector3.MoveTowards(gunHolder.localPosition, gunStartPos, adsSpeed * Time.deltaTime);
    362.         }
    363.  
    364.         if(Input.GetMouseButtonUp(1))
    365.         {
    366.             CameraController.instance.ZoomOut();
    367.         }
    368.     }
    369.  
    370.     public void Bounce(float bounceForce)
    371.     {
    372.         bounceAmount = bounceForce;
    373.         bounce = true;
    374.     }
    375. }
    i use physics.checksphere for ground check so idk why i can climb walls
     
    Last edited: Jun 28, 2021
  2. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    im rly confused how i can still climb walls because the ground check doesnt even touch the wall i think, how do i show it as a gizmo?
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hi,

    How about using addForce to move the player?
    Then when it's too steep the player can't go further.
     
  5. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i dont rly want to rework my movement system tho...i think i kinda solved it but now the movement when jumping is kinda weird and once you jump, you cant control your character at all.

    heres my script
    i got rid of isgrounded variable
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public static PlayerController instance;
    7.  
    8.     [Header("Movement and Jump")]
    9.     public float moveSpeed;
    10.     public float runSpeed = 12f;
    11.     public float jumpPower;
    12.  
    13.     [HideInInspector]
    14.     public bool canJump;
    15.     private bool canDoubleJump = false;
    16.  
    17.     public Animator anim;
    18.     private Rigidbody rb;
    19.  
    20.     private Vector3 newMovePos;
    21.  
    22.     private float bounceAmount;
    23.     private bool bounce;
    24.     [HideInInspector]
    25.     public bool canWalk;
    26.  
    27.     bool isGrounded;
    28.     public Transform groundCheck;
    29.     public LayerMask ground;
    30.     public float groundDistance = 0.4f;
    31.  
    32.     [Header("Looking")]
    33.     public Transform camHolder;
    34.     public Transform camTrans;
    35.     public float mouseSens;
    36.     float verticalLookRotation = 0f;
    37.  
    38.     [Header("Shooting")]
    39.     public List<Gun> allGuns = new List<Gun>();
    40.     public List<Gun> unlockableGuns = new List<Gun>();
    41.     public Gun activeGun;
    42.     public int startGun;
    43.     private int currentGun;
    44.  
    45.     public LayerMask layersThatAffectShooting;
    46.  
    47.     [Space(25)]
    48.     public Transform firePoint;
    49.  
    50.     [Space(25)]
    51.     public Transform ADSPoint, gunHolder;
    52.     private Vector3 gunStartPos;
    53.     public float adsSpeed = 2f;
    54.  
    55.     [Space(20)]
    56.     public GameObject muzzleFlash;
    57.  
    58.     [Header("Audio")]
    59.     public AudioSource footstepFast, footstepSlow;
    60.  
    61.     private void Awake()
    62.     {
    63.         Time.timeScale = 1f;
    64.         instance = this;
    65.     }
    66.  
    67.     // Start is called before the first frame update
    68.     void Start()
    69.     {
    70.         rb = GetComponent<Rigidbody>();
    71.  
    72.         for (int i = 0; i < allGuns.Count; i++)
    73.         {
    74.             allGuns[i].gameObject.SetActive(false);
    75.         }
    76.  
    77.         currentGun = startGun;
    78.  
    79.         SwitchGun();
    80.  
    81.         gunStartPos = gunHolder.localPosition;
    82.     }
    83.  
    84.     // Update is called once per frame
    85.     void Update()
    86.     {
    87.         if (!UIController.instance.pauseMenu.activeInHierarchy && !GameManager.instance.levelEnding)
    88.         {
    89.             muzzleFlash.SetActive(false);
    90.  
    91.             //isGrounded = ;
    92.             //Debug.Log(isGrounded.ToString());
    93.  
    94.             canJump = Physics.CheckSphere(groundCheck.position, groundDistance, ground);
    95.             Debug.Log(canJump.ToString());
    96.  
    97.             Jump();
    98.             HandleADS();
    99.             Look();
    100.             Shoot();
    101.             WeaponSwitchInput();
    102.             SwitchGun();
    103.  
    104.             if(bounce)
    105.             {
    106.                 bounce = false;
    107.                 rb.AddForce(transform.up * bounceAmount, ForceMode.Impulse);
    108.  
    109.                 canDoubleJump = true;
    110.             }
    111.  
    112.             anim.SetFloat("moveSpeed", newMovePos.magnitude);
    113.             anim.SetBool("onGround", canJump);
    114.         }
    115.     }
    116.  
    117.     private void FixedUpdate()
    118.     {
    119.         if (!UIController.instance.pauseMenu.activeInHierarchy && !GameManager.instance.levelEnding)
    120.         {
    121.             Movement();
    122.         }
    123.     }
    124.  
    125.     void Movement()
    126.     {
    127.         if (canJump)
    128.         {
    129.             float x = Input.GetAxis("Horizontal");
    130.             float y = Input.GetAxis("Vertical");
    131.  
    132.             if (Input.GetKey(KeyCode.LeftShift))
    133.             {
    134.                 x *= runSpeed;
    135.                 y *= runSpeed;
    136.             }
    137.             else
    138.             {
    139.                 x *= moveSpeed;
    140.                 y *= moveSpeed;
    141.             }
    142.  
    143.             Vector3 movePos = transform.right * x + transform.forward * y;
    144.             newMovePos = new Vector3(movePos.x, rb.velocity.y, movePos.z);
    145.  
    146.             rb.velocity = newMovePos;
    147.         }
    148.     }
    149.  
    150.     void Jump()
    151.     {
    152.         //Handle Jumping
    153.         if (Input.GetKeyDown(KeyCode.Space))
    154.         {
    155.             if (canJump)
    156.             {
    157.                 rb.AddForce(transform.up * jumpPower, ForceMode.Impulse);
    158.  
    159.                 //moveInput.y = jumpPower;
    160.  
    161.                 canJump = false;
    162.                 canDoubleJump = true;
    163.  
    164.                 AudioManager.instance.PlaySFX(8);
    165.                
    166.             }
    167.             else if (canDoubleJump)
    168.             {
    169.                 rb.AddForce(transform.up * jumpPower, ForceMode.Impulse);
    170.                 //moveInput.y = jumpPower;
    171.  
    172.                 canDoubleJump = false;
    173.  
    174.                 AudioManager.instance.PlaySFX(8);
    175.             }
    176.         }
    177.     }
    178.  
    179.     void Look()
    180.     {
    181.         transform.Rotate(Vector3.up * mouseSens * Input.GetAxisRaw("Mouse X"));
    182.         verticalLookRotation -= Input.GetAxisRaw("Mouse Y") * mouseSens;
    183.         verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
    184.        
    185.         camHolder.localEulerAngles = new Vector3(verticalLookRotation, 0, 0);
    186.     }
    187.  
    188.     void Shoot()
    189.     {
    190.         switch(activeGun.fireType)
    191.         {
    192.             case FireType.SingleShot:
    193.                 {
    194.                     if (Input.GetMouseButtonDown(0) && activeGun.fireCounter <= 0)
    195.                     {
    196.                         RaycastHit hit;
    197.                         if (Physics.Raycast(camTrans.position, camTrans.forward, out hit, 50f, layersThatAffectShooting))
    198.                         {
    199.                             if (Vector3.Distance(camTrans.position, hit.point) > 2f)
    200.                             {
    201.                                 firePoint.LookAt(hit.point);
    202.                                 //Debug.Log("Fire point is looking at center");
    203.                             }
    204.                         }
    205.                         else
    206.                         {
    207.                             firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
    208.  
    209.                         }
    210.  
    211.                         FireShot();
    212.                     }
    213.                 }
    214.                 break;
    215.  
    216.             case FireType.AutoFire:
    217.                 {
    218.                     if(Input.GetMouseButton(0) && activeGun.fireCounter <= 0)
    219.                     {
    220.                         RaycastHit hit;
    221.                         if (Physics.Raycast(camTrans.position, camTrans.forward, out hit, 50f, layersThatAffectShooting))
    222.                         {
    223.                             if (Vector3.Distance(camTrans.position, hit.point) > 2f)
    224.                             {
    225.                                 firePoint.LookAt(hit.point);
    226.                                 //Debug.Log("Fire point is looking at center");
    227.                             }
    228.                         }
    229.                         else
    230.                         {
    231.                             firePoint.LookAt(camTrans.position + (camTrans.forward * 30f));
    232.  
    233.                         }
    234.  
    235.                         if (activeGun.fireCounter <= 0)
    236.                         {
    237.                             FireShot();
    238.                         }
    239.                     }
    240.                     break;
    241.                 }
    242.         }
    243.  
    244.        
    245.     }
    246.  
    247.     public void FireShot()
    248.     {
    249.         if (activeGun.currentAmmo > 0)
    250.         {
    251.  
    252.             activeGun.currentAmmo--;
    253.  
    254.             Instantiate(activeGun.bullet, firePoint.position, firePoint.rotation);
    255.  
    256.             activeGun.fireCounter = activeGun.fireRate;
    257.  
    258.             UIController.instance.ammoText.text = "AMMO: " + activeGun.currentAmmo;
    259.  
    260.             muzzleFlash.SetActive(true);
    261.         }
    262.     }
    263.  
    264.     private void WeaponSwitchInput()
    265.     {
    266.         int previousSelectedWeapon = currentGun;
    267.  
    268.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    269.         {
    270.             if (currentGun >= allGuns.Count - 1)
    271.                 currentGun = 0;
    272.             else
    273.                 currentGun++;
    274.         }
    275.  
    276.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    277.         {
    278.             if (currentGun <= 0)
    279.                 currentGun = allGuns.Count - 1;
    280.             else
    281.                 currentGun--;
    282.         }
    283.  
    284.         if(Input.GetKeyDown(KeyCode.Alpha1))
    285.         {
    286.             currentGun = 0;
    287.         }
    288.  
    289.         if (Input.GetKeyDown(KeyCode.Alpha2) && allGuns.Count >= 2)
    290.         {
    291.             currentGun = 1;
    292.         }
    293.  
    294.         if (Input.GetKeyDown(KeyCode.Alpha3) && allGuns.Count >= 3)
    295.         {
    296.             currentGun = 2;
    297.         }
    298.  
    299.         if (Input.GetKeyDown(KeyCode.Alpha4) && allGuns.Count >= 4)
    300.         {
    301.             currentGun = 3;
    302.         }
    303.         if (previousSelectedWeapon != currentGun)
    304.             SwitchGun();
    305.     }
    306.  
    307.     private void SwitchGun()
    308.     {
    309.         int i = 0;
    310.         foreach(Gun weapon in allGuns)
    311.         {
    312.             weapon.gameObject.SetActive(i == currentGun);
    313.             i++;
    314.         }
    315.  
    316.         activeGun = allGuns[currentGun];
    317.         UIController.instance.ammoText.text = "AMMO: " + activeGun.currentAmmo;
    318.  
    319.         firePoint.position = activeGun.firePoint.position;
    320.     }
    321.  
    322.     public void AddGun(string gunToAdd)
    323.     {
    324.         bool gunUnlocked = false;
    325.  
    326.         if (unlockableGuns.Count > 0)
    327.         {
    328.             for (int i = 0; i < unlockableGuns.Count; i++)
    329.             {
    330.                 if (unlockableGuns[i].gunName == gunToAdd)
    331.                 {
    332.                     gunUnlocked = true;
    333.  
    334.                     allGuns.Add(unlockableGuns[i]);
    335.  
    336.                     unlockableGuns.RemoveAt(i);
    337.  
    338.                     i = unlockableGuns.Count;
    339.  
    340.                     Debug.Log("Adding " + gunToAdd);
    341.                 }
    342.             }
    343.         }
    344.  
    345.         if (gunUnlocked)
    346.         {
    347.             currentGun = allGuns.Count - 1;
    348.  
    349.         }
    350.     }
    351.  
    352.     void HandleADS()
    353.     {
    354.         if(Input.GetMouseButton(1))
    355.         {
    356.             CameraController.instance.ZoomIn(activeGun.zoomAmount);
    357.             gunHolder.position = Vector3.MoveTowards(gunHolder.position, ADSPoint.position, adsSpeed * Time.deltaTime);
    358.         }
    359.         else
    360.         {
    361.             gunHolder.localPosition = Vector3.MoveTowards(gunHolder.localPosition, gunStartPos, adsSpeed * Time.deltaTime);
    362.         }
    363.  
    364.         if(Input.GetMouseButtonUp(1))
    365.         {
    366.             CameraController.instance.ZoomOut();
    367.         }
    368.     }
    369.  
    370.     public void Bounce(float bounceForce)
    371.     {
    372.         bounceAmount = bounceForce;
    373.         bounce = true;
    374.     }
    375. }
     
  6. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    If you dont want to use the physics you could perhaps use the forward slope angle to reduce the speed instead? The steeper there is, the lower speed. Something like
    Vector3 movePos = transform.right * x + transform.forward * y * Mathf.Clamp01((90f - playerForwardGroundAngle) / 90f);
     
    Last edited: Jun 30, 2021