Jumping cancels out x and z velocity.
So Im working on a movement script. Im using addforce for movement so that I can have acceleration, the only problem is that when I jump, my momentum going forward is reset. The bigger problem is that if i jump and try and move forward, my character decelerates until I can't move at all until I land. I need a way so that the jumping carries over the momentum. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public Rigidbody rb;
public Transform cam;
public float speed = 15f;
public float maxSpeed = 10f;
public float gravity = -10f;
public float jumpPower = 10;
public float turnSmoothTime = 0.01f;
float turnSmoothVelocity;
public Animator anim;
float velocity = 0f;
//ground
public Transform groundCheck;
public LayerMask groundMask;
bool grounded;
//ground comformity
public Transform objectToPlace;
// Update is called once per frame
private void Update()
{
//hide the cursor
if (Input.GetMouseButtonDown(2))
{
Cursor.visible = false;
}
grounded = Physics.CheckBox(groundCheck.position, new Vector3(0.4f, 0.2f, 0.4f), rb.rotation, groundMask);
if (Input.GetButtonDown("Jump") && grounded)
{
rb.velocity = (new Vector3(0f, jumpPower, 0f));
}
}
void FixedUpdate()
{
//gravity
rb.AddForce(new Vector3(0f, gravity, 0f));
float xinput = Input.GetAxisRaw("Horizontal");
float yinput = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(xinput, 0f, yinput).normalized;
//movement
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
rb.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
//this code makes it so that I dont go too fast, and the tempY thing is because the jumpheight was too lower and more floaty when moving
float tempY = rb.velocity.y;
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
rb.velocity = new Vector3(rb.velocity.x, tempY, rb.velocity.z);
//this aplies the movement
rb.AddForce(moveDir * speed);
//ground comformity
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hitInfo;
float maxDistance = 1.5f;
if(grounded)
{
maxDistance = 5f;
}
if (Physics.Raycast(ray, out hitInfo, maxDistance, groundMask))
{
rb.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal) * rb.rotation;
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
}
else
{
Debug.DrawLine(ray.origin, ray.origin + ray.direction * 1.5f, Color.green);
}
}
//animation
anim.SetFloat("Speed", Mathf.Abs(direction.x) + Mathf.Abs(direction.z));
anim.SetBool("Jumpy?", grounded);
}
}
Comment
Your answer
Follow this Question
Related Questions
Player stuck when jumping into a wall 0 Answers
Player going through walls 1 Answer
Problem with rigidbody 2 Answers
Determine the direction an object is actually moving? 0 Answers