#physics and character controller
1 messages · Page 1 of 1 (latest)
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.
should i show a screen recording?
yeah something like this
i'll take a look, but idk if seeing the effect will really identify the cause.
yeah, i don't see the pause you talked about
```cs
your code
```
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();
}
}
moveDirection might want to normalize this or players will move faster diagonally
i don't see anything in that code which responds to collision events.
is the physics system handling it?
oh, so you normalize it twice? heh
twice in code, but once when it's running.
if you mean i use a rigid body? yeah
yea
well then it's likely something in your rigid body settings, or your friction.
the friction changes depending on if its on the gorund or not, maybe its that?
yeah.... what happens if you lower friction values?
same thing happens when i set drag from 5 to 0
i have no idea thats what i was asking you
ive tried changing the drag and gravity
heres the settings
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.
gl!