Search Unity

Character Controller Bounce

Discussion in '2D' started by piggybank1974, Jul 23, 2019.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    This is the first time I've used a character controller, in any project or prototype I've written, but I thought it might be the best option as I wanted the character to go up slopes, and I couldn't get my other controller to work correctly in that situation, sometimes "see below for the code"

    Anyway as you can see from the screenshot I've created a little prototype with a capsule and added a character controller script plus my own script to it.

    I've created a form of rails so this can stop the player "capsule" falling off the map if it needed.

    but what I want to do is if the player hits these rails too hard "velocity" I want the player to bounce off, in the perpendicular direction I know you cannot use a rigidbody or shouldn't and physics materials don't work with the character controller either as you don't use a rigidbody as I said.

    Code (CSharp):
    1.    
    2. float mass = 3.0F;
    3.    Vector3 impact = Vector3.zero;
    4.  
    5.   private void Update()
    6.   {
    7.    if (impact != Vector3.zero)
    8.      {
    9.       Character.Move(impact * Time.deltaTime);
    10.       impact = Vector3.Lerp(impact, Vector3.zero, 10 * Time.deltaTime);
    11.       //return;
    12.     }
    13.  
    14.    MoveDirection = new Vector3(Input.GetAxis("Horizontal") * MoveSpeed, MoveDirection.y, Input.GetAxis("Vertical") * MoveSpeed);
    15.  
    16.    if (Character.isGrounded == true)
    17.     if (Input.GetButtonDown("Jump") == true)
    18.       {
    19.        MoveDirection.y = JumpForce;
    20.       }
    21.  
    22.    MoveDirection.y = MoveDirection.y + Physics.gravity.y * GravityScale;  
    23.    Character.Move(MoveDirection * Time.deltaTime);
    24.   }
    25.  
    26.   private void OnControllerColliderHit(ControllerColliderHit hit)
    27.   {
    28.    if (hit != null && hit.gameObject.CompareTag("Rail") == true)
    29.      {
    30.       //Vector3.Cross(hit.normal, -Vector3.right)
    31.       AddImpact(hit.normal, 50);
    32.       //Debug.Log("Player hit by " + hit.gameObject.name);
    33.      }
    34.   }
    35.  
    36.   public void AddImpact(Vector3 dir, Single force)
    37.   {
    38.    impact += dir.normalized * force / mass;
    39.   }
    40.  
    It does bounce but if the player is still holding the keys down, it moves again to fast, also but a short delay so the Impact.zero would need to happen, but it does not feel like the physics Material bounce.

    My Slope code On my other controller test.

    Code (CSharp):
    1.  
    2. public class PlayerComponent : MonoBehaviour
    3. {
    4. private RaycastHit mHit;
    5. public LayerMask Masks;
    6. public GameObject GroundObject;
    7. public float OffsetY = 1.27F;
    8.    
    9.     void Update()
    10.     {
    11.      Debug.DrawLine(this.transform.position, this.transform.position + -this.transform.up * 0.27F, Color.yellow, 3F);
    12.      if (Physics.Raycast(this.transform.position, -this.transform.up, out mHit, 3, Masks.value) == true)
    13.        {
    14.         transform.up -= (transform.up - mHit.normal) * 0.1f;
    15.    transform.position = new Vector3(transform.position.x, mHit.point.y + OffsetY, transform.position.z);        
    16.         Debug.Log("Player on ground: " + mHit.collider.gameObject.name);
    17.        }
    18.  
    19. }
    20. }
    21.  
    I was manually moving this in player mode.



    The code I used for my own Slope test player component is here like I said this sort of worked until I got about 80% to the top of the slope where the Checkground found the bottom ground and not the one the player was under.
     

    Attached Files: