- Home /
Question by
DragonSlays · Nov 20, 2019 at 10:43 AM ·
script.jump
I'm having trouble getting my character to jump (2D)
This is the script I currently have and I'm having trouble finding a jump script that works with it
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
private float maxSpeed = 5f;
private bool facingLeft = true;
private Rigidbody2D rb;
private Animator anim;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(h * maxSpeed, rb.velocity.y);
anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
if (h > 0 && !facingLeft)
reverseImage();
else if (h < 0 && facingLeft)
reverseImage();
}
void reverseImage()
{
facingLeft = !facingLeft;
Vector2 theScale = rb.transform.localScale;
theScale.x *= -1;
rb.transform.localScale = theScale;
}
}
Comment
Answer by harsh2580 · Nov 20, 2019 at 12:07 PM
float jumpForce = 10; // Change this according to ur need
if (Input.GetKeyDown(KeyCode.Space) & grounded) { jumping = true; rb.AddForce(jumpForce * Time.deltaTime); }
Your answer
Follow this Question
Related Questions
Can not double jump when i fall off platform 1 Answer
Jump when you press on-screen button 1 Answer
Endless Staircase 1 Answer
Character won't jump. 0 Answers
Parameter 'Jump' does not exist 1 Answer