Question by
kim12345678 · Jun 23, 2017 at 04:19 AM ·
c#rigidbody2djump
Rigidbody is not Jumping while Moving
Help please. My Rigidbody2d isn't jumping while pushing horizontels/Keys,.... i've got no more ideas how to fix it.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float maxSpeed = 4;
public float jumpForce = 550;
public Transform groundCheck;
[HideInInspector]
public bool lookingLeft = true;
public LayerMask whatIsGround;
private Rigidbody2D rb2d;
private Animator anim;
private bool isGrounded = false;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
isGrounded = false;
anim.Play("Springen");
}
isGrounded = true;
}
void FixedUpdate()
{
anim.Play("Idle");
float hor = Input.GetAxis("Horizontal");
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.15F, whatIsGround);
anim.SetBool("isGrounded", isGrounded);
if (hor > 0 || hor < 0)
{
anim.Play("Laufen");
anim.SetFloat("Speed", Mathf.Abs(hor));
rb2d.velocity = new Vector2(hor * maxSpeed, rb2d.velocity.y);
}
if ((hor < 0 && !lookingLeft) || (hor > 0 && lookingLeft))
{
Flip();
}
}
public void Flip()
{
lookingLeft = !lookingLeft;
transform.localRotation = Quaternion.Euler(0, transform.localRotation.eulerAngles.y + 180, 0);
}
}
Comment