- Home /
Question by
Joker42000 · Nov 15, 2018 at 09:20 PM ·
controllerjumpingplayer movement2d platformer
2d platfromer player controller will not jump
I have checked the inputs and can not get the jumping to work. The movement does work for left and right.`using UnityEngine; using System.Collections; using UnityEngine.UI;
public class SimpleController : MonoBehaviour {
[HideInInspector] public bool facingRight = true;
[HideInInspector] public bool jump = false;
public float moveForce = 365f;
public float maxSpeed = 5f;
public float jumpForce = 400f;
public Transform groundCheck;
public Text countText;
public Animator animator;
private bool grounded = false;
private Animator anim;
private Rigidbody2D rb2d;
private int count;
void start ()
{
count = 0;
SetCountText();
}
// Use this for initialization
void Awake()
{
anim = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetButtonDown("Jump") && grounded)
{
jump = true;
}
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(h));
if (h * rb2d.velocity.x < maxSpeed)
rb2d.AddForce(Vector2.right * h * moveForce);
if (Mathf.Abs(rb2d.velocity.x) > maxSpeed)
rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
if (h > 0 && !facingRight)
Flip();
else if (h < 0 && facingRight)
Flip();
if (jump)
{
anim.SetTrigger("Jump");
rb2d.AddForce(new Vector2(0f, jumpForce));
jump = false;
}
animator.SetFloat("Speed", Mathf.Abs(h));
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OntriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count; " + count.ToString();
}
}`
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612182216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D: Properly Implementing Player Movement with a dash, jump, and knockback? 2 Answers
Move Object a direction with one button and move the object back with the same button? 1 Answer
(C#) Accelerating Player Based On Distance 2 Answers
Problem with rigidbody jumping off of platform. 0 Answers
Play animation with FPS controller? 2 Answers