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

2d movement


var motion = Vector2()
var speed  = 150
var gravity = 30
var jump=-600
var double_jump_power = -750
var can_double_jump = false
func _ready():
	
	pass
func _physics_process(delta):
	Double_jump_and_jump()
	if Input.is_action_pressed("ui_right"):
		motion.x = speed 
		A.play("run")
		A.flip_h = false
	elif Input.is_action_pressed("ui_left"):
		motion.x = -speed
		A.play("run")
		A.flip_h = true
	else:
		motion.x = 0
	if is_on_floor()==false:
		motion.y +=  gravity
	move_and_slide(motion,Floor)
func Double_jump_and_jump():
	if is_on_floor()==true:
		can_double_jump=true
	if Input.is_action_just_pressed("ui_up")&& is_on_floor()==true:
		motion.y = jump
	#double_jump if you dont like it just remove these lines of code downhere 
	if is_on_floor() ==false && motion.y > 0 && can_double_jump==true&& Input.is_action_just_pressed("ui_up"):
		motion.y = double_jump_power
		can_double_jump=false
	pass
#plz if this helped you donate
#or check out my NFT discord server if you want to help me https://discord.gg/YK6BqdbT
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to print dictionary in c# 
Csharp :: how to make a mouse down condition in unity 
Csharp :: linux command line switch statement 
Csharp :: cast int to enum type c# 
Csharp :: how to redirect to extern page in .net core 
Csharp :: c# do while loop 
Csharp :: smtp check if email sent 
Csharp :: how to get hours and minutes from second in c# 
Csharp :: convert.tostring with datetime string 
Csharp :: c# remove double quotes from string 
Csharp :: get layermask from gameobject layer unity 
Csharp :: how to make panel scrollable c# 
Csharp :: unity log warning 
Csharp :: unity always rotating object 
Csharp :: c# datagridview cell click event 
Csharp :: c# find all indexes 
Csharp :: failed to read the request form. missing content-type boundary .net core 
Csharp :: c# string contains any of list 
Csharp :: .net mvc decimal displayformat currency 
Csharp :: visual studio console clear 
Csharp :: c# get char from string 
Csharp :: gameobject on click unity 
Csharp :: clamp vector3 unity 
Csharp :: c# sort array of objects 
Csharp :: c# read lines number 3 from string 
Csharp :: c# return list in descending order 
Csharp :: join two array c# 
Csharp :: xmldocument to c# object 
Csharp :: raycasthit unity 
Csharp :: how to play multiple sound at once on c# windows form 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =