- Home /
Jump in direction while in air?
So i have a character that moves well and jumps fine while in idle but when it moves and jumps it stops running and jumps without contentiously moving, i have a 3rd person camera following the player from an angle .. any help? here's my code
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
public float jumpSpeed = 5f;
public float verticalVelocity = 0f;
public float jumpForce = 5f;
public bool grounded;
private Animator anim;
private HashIDs hash;
Rigidbody rb;
void Awake ()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
}
void FixedUpdate ()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
MovementManagement(h, v);
//
}
void Update ()
{
if(grounded && Input.GetButtonDown("Jump")){
Jump();
}
}
public void Jump(){
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rb.AddForce( new Vector3(h, 10, v), ForceMode.Impulse );
rb.velocity += Vector3.up * jumpSpeed;
grounded = false;
anim.SetBool("Jump", true);
}
void OnCollisionEnter(Collision collision) {
grounded= true;
anim.SetBool("Jump", false);
}
void MovementManagement (float horizontal, float vertical)
{
if(horizontal != 0f || vertical != 0f)
{
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
anim.SetFloat(hash.speedFloat, 0);
}
public void Rotating (float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.MoveRotation(newRotation);
}
}
Answer by TRG96 · Aug 31, 2014 at 10:46 AM
Have
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
outside of the Jump() function. rb.AddForce( new Vector3(h, 10, v), ForceMode.Impulse );
only gets called once when the character is grounded. Have a separate function which deals with the in air movement when the character is not grounded. something like
void Update() { if(!grounded) { h = Input.GetAxis("horizontal"); v = //vertical axis rb.velocty = new Vector(h,0,v) } }
Your answer
Follow this Question
Related Questions
jump in air 0 Answers
In air movement troubles. 1 Answer
Weird behaviour in standalone but not in editor 1 Answer
In Air Movement 0 Answers