- Home /
Question by
matejaizverieas · Oct 04, 2020 at 02:49 PM ·
c#rigidbody.addforce
The characters Rigidbody, falls straight down when movement input is 0 and in the air. C#
I'm currently working on a 3 fps game and I have an issue with the player's movement. when the player is jumping while moving and the movement input is 0 right after the jump, then the player stops in the air and moves upward slightly and then falls straight down(as if hitting an invisible wall). The player only has a Rigidbody and a looking script.
Move input is 0 halfway through the jump
Move input is more than 0 through the jump
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Movement
[Header("Movement")]
public float speedometer;
[SerializeField, Range(1f, 40f)] float GroundSpeed = 10;
[SerializeField, Range(0f, 20f)] float AirSpeed = 5;
float maxSpeed ;
public Vector3 velocity, desiredVelocity, velocityChange;
public float rayHitdistance = 2;
public LayerMask whatIsGround;
//Jump
[Header("Jump")]
[SerializeField, Range(0f, 20f)] float jumpHight = 5;
// Inputs
float inputX, inputZ;
bool inputJump;
Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Start()
{
maxSpeed = GroundSpeed;
}
void Inputs()
{
inputX = Input.GetAxisRaw("Horizontal");
inputZ = Input.GetAxisRaw("Vertical");
inputJump |= Input.GetButtonDown("Jump");
}
// Update is called once per frame
void Update()
{
Inputs();
speedometer = rb.velocity.magnitude;
}
void FixedUpdate()
{
MoveVelocity();
if (inputJump && OnGround())
{
Jump();
inputJump = false;
}
}
void Jump()
{
float jumpForce = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHight);
rb.AddRelativeForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void MoveVelocity()
{
float speed = OnGround() ? GroundSpeed : AirSpeed;
desiredVelocity = new Vector3(inputX, 0f, inputZ).normalized * speed;
desiredVelocity = transform.TransformDirection(desiredVelocity);
velocity = rb.velocity;
velocityChange = (desiredVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -speed, speed);
velocityChange.z = Mathf.Clamp(velocityChange.z, -speed, speed);
velocityChange.y = 0;
rb.AddForce(Vector3.ClampMagnitude(velocityChange, maxSpeed), ForceMode.VelocityChange);
}
bool OnGround()
{
if (Physics.Raycast(transform.position, Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0.5f, 0, 0), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(-0.5f, 0, 0), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0, 0, 0.5f), Vector3.down, rayHitdistance, whatIsGround) ||
Physics.Raycast(transform.position - new Vector3(0, 0, -0.5f), Vector3.down, rayHitdistance, whatIsGround))
{
return true;
}
else
{
return false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Having trouble adding force to rigidbodies after collision 0 Answers
Rigidbody jumpPad Not working 0 Answers