xxxxxxxxxx
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;
private bool isWallSliding;
private float wallSlidingSpeed = 2f;
private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.2f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.4f;
private Vector2 wallJumpingPower = new Vector2(8f, 16f);
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
private void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
WallSlide();
WallJump();
if (!isWallJumping)
{
Flip();
}
}
private void FixedUpdate()
{
if (!isWallJumping)
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private bool IsWalled()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}
private void WallSlide()
{
if (IsWalled() && !IsGrounded() && horizontal != 0f)
{
isWallSliding = true;
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
else
{
isWallSliding = false;
}
}
private void WallJump()
{
if (isWallSliding)
{
isWallJumping = false;
wallJumpingDirection = -transform.localScale.x;
wallJumpingCounter = wallJumpingTime;
CancelInvoke(nameof(StopWallJumping));
}
else
{
wallJumpingCounter -= Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
{
isWallJumping = true;
rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
wallJumpingCounter = 0f;
if (transform.localScale.x != wallJumpingDirection)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
Invoke(nameof(StopWallJumping), wallJumpingDuration);
}
}
private void StopWallJumping()
{
isWallJumping = false;
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
}
//Huge credits to bendux. Check out his yt
xxxxxxxxxx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// this has jumping, movement, and the code for groundCheck. You must do
// things yourself in the unity editor to for groundCheck. Code Tested
// unity 2d platformer
public float speed = 5f;
public float jumpSpeed = 3f;
private float direction = 0f;
private Rigidbody2D player;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask groundLayer;
private bool isTouchingGround;
// Start is called before the first frame update
void Start()
{
player = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isTouchingGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
direction = Input.GetAxis("Horizontal");
if (direction > 0f)
{
player.velocity = new Vector2(direction * speed, player.velocity.y);
}
else if (direction < 0f)
{
player.velocity = new Vector2(direction * speed, player.velocity.y);
}
else
{
player.velocity = new Vector2(0, player.velocity.y);
}
if (Input.GetButtonDown("Jump") && isTouchingGround)
{
player.velocity = new Vector2(player.velocity.x, jumpSpeed);
}
}
}
xxxxxxxxxx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class 2dMovement : MonoBehaviour
{
//This also works with joystick, PS this is topdown movement
private Rigidbody2D rb;
public float MoveSpeed = 15f;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void Update ()
{
private float vertical;
private float horizontal;
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * MoveSpeed, vertical * MoveSpeed);
}
}
xxxxxxxxxx
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5.0f; // Adjust this value to control the player's movement speed.
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
// Get input from arrow keys.
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Calculate the movement vector.
Vector2 movement = new Vector2(horizontalInput, verticalInput).normalized * moveSpeed;
// Apply the movement to the Rigidbody.
rb.velocity = movement;
}
}
xxxxxxxxxx
if (!Player_Jump && !InAirNoCollision(pb_Player))
{
Force = Gravity;
Player_Jump = true;
}
xxxxxxxxxx
private void timer_Jump_Tick(object sender, EventArgs e)
{
if (Force > 0)
{
if (Collision_Bottom(pb_Player))
{
Force = 0;
}
else
{
Force--;
pb_Player.Top -= Speed_Jump;
}
}
else
{
Player_Jump = false;
}
}
xxxxxxxxxx
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
Player_Left = true;
break;
case Keys.Right:
Player_Right = true;
break;
}
}
xxxxxxxxxx
private void timer_Gravity_Tick(object sender, EventArgs e)
{
if (!Player_Jump && pb_Player.Location.Y +
pb_Player.Height < WorldFrame.Height - 2 && !Collision_Top(pb_Player))
{
pb_Player.Top += Speed_Fall;
}
if (!Player_Jump && pb_Player.Location.Y + pb_Player.Height > WorldFrame.Height - 1)
{
pb_Player.Top--;
}
}