Change rigibody velocity without affecting other axis
So, I'm trying to make a FPS Shooter with rigidbody but I don't know how to use velocity without affecting the Y axis, I want the player to jump and fall naturally but using velocity makes him fall really slow. I need help with that.`public class PlayerMovement : MonoBehaviour { public Rigidbody rb; public float moveSpeed = 5f; public float jumpHeight; public float mouseSensitivy = 10f;
private Vector3 moveInput;
private Vector3 mouseInput;
public bool canDoubleJump;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 vel = rb.velocity;
moveInput = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, vel.y * jumpHeight, Input.GetAxis("Vertical") * moveSpeed);
Vector3 moveHorizontal = transform.right * moveInput.x;
Vector3 moveVertical = transform.forward * moveInput.z;
rb.velocity = (moveHorizontal + moveVertical) * Time.fixedDeltaTime;
mouseInput = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivy * Time.deltaTime;
if (isGrounded())
{
canDoubleJump = true;
}
if (Input.GetButtonDown("Jump"))
{
if (isGrounded())
{
rb.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
}
else if (canDoubleJump)
{
rb.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
canDoubleJump = false;
}
}
bool isGrounded()
{
return transform.Find("GroundCheck").GetComponent<GroundCheck>().isGrounded;
}
}
}`
Your answer
Follow this Question
Related Questions
Compensate slow fall when using rigidbody.drag 0 Answers
First Person Slide in direction of rigidbody movement and not look direction 1 Answer
Why is new Vector3 stronger when player is in the air? 0 Answers
How can i move a rigidbody according to its local axis? 0 Answers
Implementing falling: How to maintain forward inertia as I transition into a fall animation? 0 Answers