Question by
Bebekx · Jul 12, 2021 at 09:52 PM ·
scripting problemjump
My 2D character cant jump (UNITY 2020.3)
I created script to move my character. It can run left and right but it cant jump. I tried a lot of methods but no one worked...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 10.0f;
public Rigidbody2D rigidbody2d;
float horizontal;
bool jump = false;
public Animator gracz;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown("space"))
{
jump = true;
}
}
void FixedUpdate()
{
if (Input.GetKey("a") == true)
{
gracz.SetBool("stanieLewa", false);
horizontal = -1;
gracz.SetBool("biegLewa", true);
gracz.SetBool("chodzenie", false);
}
else if (Input.GetKey("d") == true)
{
gracz.SetBool("stanieLewa", false);
horizontal = 1;
gracz.SetBool("chodzenie", true);
gracz.SetBool("biegLewa", false);
}
else
{
if (horizontal == -1)
{
gracz.SetBool("stanieLewa", true);
}
horizontal = 0;
gracz.SetBool("chodzenie", false);
gracz.SetBool("biegLewa", false);
}
if (jump)
{
rigidbody2d.AddForce(new Vector2(0, 40));
jump = false;
}
Vector2 position = rigidbody2d.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
rigidbody2d.MovePosition(position);
}
}
Its my second game ever. Pls quick help
Comment