#physics and character controller

1 messages · Page 1 of 1 (latest)

jovial forge
#

im really new to unity made a player controller for a 1st person 3d game, but whenever it touches a wall it stalls the character in the air, and when it hits the ground the character loses a little bit of speed suddenly before accellerating again. whats up with that

gloomy current
#

what do you mean, "stalls in the air"?

#

sounds like on any contact with any surface velocity is immediately set to zero and it takes a moment to recover.

jovial forge
#

should i show a screen recording?

gloomy current
#

i'll take a look, but idk if seeing the effect will really identify the cause.

jovial forge
#

terrible video quality

gloomy current
#

yeah, i don't see the pause you talked about

jovial forge
#

sorry

#

should i share the code?

#

actually how do i share code into discord

gloomy current
#

```cs
your code
```

jovial forge
#
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Movement")]
    public float moveSpeed;

    public float groundDrag;

    public float jumpForce;
    public float jumpCooldown;
    public float airMultiplier;
    public bool readyToJump;

    

    private Vector3 respawn;

    [Header("Keybinds")]
    public KeyCode jumpKey = KeyCode.Space;

    [Header("Ground Check")]
    public float playerHeight;
    public LayerMask whatIsGround;
    bool grounded;

    public Transform orientation;

    float horInput;
    float vertInput;

    Vector3 moveDirection;

    Rigidbody rb;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;
        readyToJump = true;

        respawn = transform.position;
}

    // Update is called once per frame
    void Update()
    {
        //check if on the ground using rays
        grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
        
        
        MyInput();
        SpeedLimit();

        if (transform.position.y < -6)
            transform.position = respawn;

        //drag for physics
        if (grounded)
            rb.drag = groundDrag;
        else
            rb.drag = 0;
        

    }


#
    {
        MovePlayer();
    }

    private void MovePlayer()
    {
        //calculate movement direction

        moveDirection = orientation.forward * vertInput + orientation.right * horInput;

        //when on ground
        if (grounded)
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);

        //when in air
        else if (!grounded)
            rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    }

    private void SpeedLimit()
    {
        Vector3 flatvel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        //limit velocity
        if (flatvel.magnitude > moveSpeed)
        {
            Vector3 limitedVel = flatvel.normalized * moveSpeed;
            rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
        }
    }
    private void MyInput()
    {
        horInput = Input.GetAxisRaw("Horizontal");
        vertInput = Input.GetAxisRaw("Vertical");

        //when to jump
        if (Input.GetKey(jumpKey) && readyToJump && grounded)
        {

            readyToJump = false;

            Jump();

            Invoke(nameof(ResetJump), jumpCooldown);
        }
    }
    private void Jump()
    {
        // reset Y velocity
        rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

        rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    }

    private void ResetJump()
    {
        readyToJump = true;
    }


}
#

and this is for a some colliders that make a cylinder shaped collider

#
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (triggered == 2)
        {
            //inside cylinder collider
        }
        else
        {
            //outside cylinder collider
        }
    }
    ushort triggered = 0;

    void OnTriggerEnter()
    {
        triggered++;
    }

    void OnTriggerExit()
    {
        triggered--;


        Update();
    }
}
gloomy current
#

moveDirection might want to normalize this or players will move faster diagonally

jovial forge
#

it is

#

i think

gloomy current
#

i don't see anything in that code which responds to collision events.

jovial forge
gloomy current
#

is the physics system handling it?

#

oh, so you normalize it twice? heh

#

twice in code, but once when it's running.

jovial forge
#

yea

gloomy current
#

well then it's likely something in your rigid body settings, or your friction.

jovial forge
#

the friction changes depending on if its on the gorund or not, maybe its that?

gloomy current
#

yeah.... what happens if you lower friction values?

jovial forge
#

same thing happens when i set drag from 5 to 0

gloomy current
#

then we know it's not drag related.

#

what are the next likely candidates?

jovial forge
#

i have no idea thats what i was asking you

#

ive tried changing the drag and gravity

#

heres the settings

gloomy current
#

ah well then I'm out of ideas and I'm sorry. I can't actually see the problem in the video or in the code.

jovial forge
#

yeah figured

#

thanks for trying

gloomy current
#

gl!