Answers for "2d player movement"

C#
0

2d player movement unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [Header("Player Movement")]
    private Rigidbody2D rb;
    [SerializeField] private float speed, jumpForce, checkRadius;
    private float input;
    [SerializeField] private Transform groundCheck;
    private bool isGround;
    [SerializeField] private LayerMask groundLayerMask;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        isGround = Physics2D.OverlapCircle(groundCheck.position, checkRadius, groundLayerMask);
        input = Input.GetAxisRaw("Horizontal");
    }
    void FixedUpdate(){
        rb.velocity = new Vector2(input * speed * Time.deltaTime, rb.velocity.y);
        if (isGround && Input.GetKey(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce * Time.deltaTime;
        }
    }
}
Posted by: Guest on September-04-2021
0

2d player movement unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 2DMovementUnityGrepper : MonoBehaviour
{
	public Rigidbody2D rb = new Rigidbody2D();
	public float h;
    public float v;
    public float moveSpeed = 5f;
	void Update()
    {
    	h = Input.GetAxisRaw("Horizontal");
        v = Input.GetAxisRaw("Vertical");
    }
    private void FixedUpdate()
    {
    	rb.velocity = new Vector2 (h * moveSpeed, v  * moveSpeed);
    }
    
    // THERE YOU GO! 2D PLAYER MOVEMENT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// BYE GREPPER USERS

}
Posted by: Guest on October-23-2021

C# Answers by Framework

Browse Popular Code Answers by Language