My character movement is not performing as expected after a jump
Please have a look at this video: https://youtu.be/5rIoFqtl1WQ
As you can see, every time my player finishes a jump, something weird happens, and there is like a glitch.
This is my code for the controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
bool jumped;
public float moveForce = 10f;
public float maxSpeed = 2f;
public float jumpForce = 300f;
public Transform groundCheck;
private bool grounded = false;
private Animator anim;
private Rigidbody2D rb2d;
private Camera camera;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
camera = FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update () {
Debug.DrawLine(transform.position, groundCheck.position, Color.magenta);
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if(Input.GetKeyDown(KeyCode.Space) && grounded){
jumped = true;
}
camera.transform.position = new Vector3(transform.position.x + 1.2f, camera.transform.position.y, camera.transform.position.z);
}
void FixedUpdate(){
float speed = 1f;
if (speed * rb2d.velocity.x < maxSpeed)
rb2d.AddForce(Vector2.right * speed * moveForce);
if (Mathf.Abs (rb2d.velocity.x) > maxSpeed)
rb2d.velocity = new Vector2(Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
anim.SetFloat("vSpeed",rb2d.velocity.y, 1f, Time.smoothDeltaTime * 50f);
anim.SetBool("Ground", grounded);
if (jumped){
rb2d.AddForce(Vector2.up * jumpForce);
jumped = false;
}
}
}
Can you see what might be causing this?
Thanks!
Comment
Your answer
Follow this Question
Related Questions
Stickmen Animations: Physic problem? 0 Answers
How to add a dropping animation (in a 2D game). 1 Answer
Top-down 2D - crawling script 0 Answers
Please Help 2D Ragdoll Physics and Skeletal Anim/Bone,Please Help Skeletal 2D and Ragdoll Physics 0 Answers
Can unity dynamically adjust a custom physics outline with animation? 0 Answers