Search Unity

Why is My Character Able to Push into the Floor?

Discussion in 'Physics' started by AnimusRex, May 15, 2019.

  1. AnimusRex

    AnimusRex

    Joined:
    Apr 26, 2019
    Posts:
    67
    Hi all,

    I have a player character that for some reason is able to push himself into the floor if he goes against the underside of a ramp.

    I'm using a rigidbody, using gravity, not kinematic. I've tried all the collision detection options, and I have rotation on the rigidbody frozen in every direction.

    I'm also using a capsule collider.

    In addition to this; my moveSpeed and jumpForce variables don't affect either the speed or the jump height at all, and I'm not sure why. Any insight is appreciated.

    This is the script I'm using;

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     //Movement VAR's
    10.     public float moveSpeed = 5f;
    11.     public float jumpForce = 5f;
    12.     public Rigidbody rigid;
    13.     public Vector3 Direction;
    14.  
    15.     Animator mAnim;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         rigid = GetComponent<Rigidbody>();
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.     }
    27.     // Update is called once per frame
    28.     void FixedUpdate()
    29.     {
    30.         Animation();
    31.         Movement();
    32.     }
    33.  
    34.     void Animation()
    35.     {
    36.         mAnim = GameObject.FindWithTag("Player").GetComponent<Animator>();
    37.     }
    38.  
    39.     void Movement()
    40.     {
    41.         float moveHorizontal = Input.GetAxis("Horizontal");
    42.         float moveVertical = Input.GetAxis("Vertical");
    43.  
    44.         Vector3 jump = new Vector3(0, jumpForce, 0);
    45.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    46.  
    47.         if (Input.GetButtonDown("Jump"))
    48.         {
    49.             rigid.AddForce(jump, ForceMode.Impulse);
    50.         }
    51.  
    52.         if (moveHorizontal + moveVertical != 0)
    53.         {
    54.             rigid.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.2F);
    55.         }
    56.  
    57.         Vector3 nextpos = transform.position;
    58.  
    59.         nextpos += movement * moveSpeed * Time.deltaTime;
    60.  
    61.         rigid.MovePosition(nextpos);
    62.  
    63.         //Animator bits
    64.         if (Math.Abs(Input.GetAxis("Horizontal")) >= Math.Abs(Input.GetAxis("Vertical")))
    65.         {
    66.             mAnim.SetFloat("Speed", Math.Abs(Input.GetAxis("Horizontal")));
    67.         }
    68.         else
    69.         {
    70.             mAnim.SetFloat("Speed", Math.Abs(Input.GetAxis("Vertical")));
    71.         }
    72.     }
    73. }
    74.  
    75.