Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

2d movement unity

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);
 }
}
Comment

how to make player movement in unity 2d

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);
        }

    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: csharp get decimal part of number 
Csharp :: c# allowedusernamecharacters 
Csharp :: Reverse Coding Challenge 1 
Csharp :: mvc session key exists 
Csharp :: multidimensional arrays c# 
Csharp :: what is unity 
Csharp :: data annotations in asp.net core 
Csharp :: draw on picturebox c# 
Csharp :: what value of combobox index c# 
Csharp :: c# function 
Csharp :: unity c# cos inverse 
Csharp :: datatable iqueryable c# linq 
Csharp :: C# round number of digits after decimal point 
Csharp :: Get Last Access Time Of Directory C# 
Csharp :: C# extract all of a property from a list of objcets 
Csharp :: how to generate a random number in c# 
Csharp :: list sum c# 
Csharp :: unity position ui element 
Csharp :: list sort c# 
Csharp :: c# get set 
Csharp :: listbox1.remove item c# 
Csharp :: c# unit test for throwing exception method 
Csharp :: c# for 
Csharp :: c sharp list 
Csharp :: entity framework with query C# 
Csharp :: c# read xml tag value 
Csharp :: c# jagged array initialization 
Csharp :: c# minimise form 
Csharp :: how to hide the title bar of window in monogame 
Csharp :: how to check url has parameter in c# 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =