I made a player movement script meant for my 2D game is there anything i should change.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public float speed = 100f;
public float JumpHeight;
public bool InAir = false;
private Rigidbody2D rb2d;
void Start() {
rb2d = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
InAir = false;
Debug.Log("InAir false");
}
private void OnCollisionExit2D(Collision2D collision)
{
InAir = true;
Debug.Log("InAir True");
}
void FixedUpdate() {
Vector2 NoMovement = new Vector2(0f, 0f);
float moveHorizontal = Input.GetAxis("Horizontal");
if (moveHorizontal > 0)
{
{
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
}
}
if (moveHorizontal < 0)
{
rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);
}
if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.UpArrow))) {
if (InAir == false)
{
rb2d.AddForce(new Vector2(0, JumpHeight), ForceMode2D.Impulse);
}
}
}
}
3 Likes
One quick tweak keep this method separate as void Movement()
2 Likes
hippity hoppity, your code is now my property
28 Likes
hms0589
5
do not add InAir bool at collision enter, when player touch wall it can jump infinte
hms0589
6
and do not add jump in fixedupdate, fixed update doesnt work very well in sudden action
doesnt work none of my keys work i just cant move with the arrow keys i cant with the wasd movment i cant i just cant
hipity hopity your code is now my property
2 Likes
left and right work but there is a delay for jumps did i do something wrong?
1 Like
can you make one for a topdown 2d game without rigidbody2d?