Answers for "How to make your player move unity 2D"

C#
1

unity 2d player move

A new jorney begins
Posted by: Guest on February-26-2021
0

How to make your player move unity 2D

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

public class Obstacle : MonoBehaviour
{

    public float moveSpeed = 8; // Speed you want your Player to move at

    Rigidbody2D myrb; // to access the Rigidbody component

    float h; // to access the Input Axis


    void Start()
    {

        myrb = GetComponent<Rigidbody2D>();

    }


    void Update()
    {

        h = Input.GetAxis("Horizontal"); // moves by 1 or -1 when pressing d or a 

        Vector2 playerPos = new Vector2(h * moveSpeed, myrb.velocity.y); // move to the right or left

        myrb.velocity = playerPos; 

    }

}
Posted by: Guest on October-09-2021

Code answers related to "How to make your player move unity 2D"

C# Answers by Framework

Browse Popular Code Answers by Language