Answers for "unity 2d horizontal movement help"

C#
0

unity 2d horizontal movement help

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

public class PlayerMovement : MonoBehaviour
{

    public float speed = 5f;
    private float movement = 0f;
    private Rigidbody2D rigidBody;
    

    // Start is called before the first frame update
    void Start()
    {

        rigidBody = GetComponent<Rigidbody2D> ();
        
    }

    // Update is called once per frame
    void Update()
    {
        movement = Input.GetAxis("Horizontal");
        if (movement > 0f)
        {
            rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
        }
        else if (movement < 0f) 
        {
            rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
        }
        else
        {
            rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
        }
        
    }
}
Posted by: Guest on June-11-2020

C# Answers by Framework

Browse Popular Code Answers by Language