Answers for "how to make 2d movement in unity"

C#
5

how to make 2d movement in unity

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

public class movement2D : MonoBehaviour
{
    Rigidbody2D body;

    float horizontal;
    float vertical;

    public float runSpeed = 10.0f;
    
    
    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();        
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);   
    }
}
Posted by: Guest on May-17-2021
6

unity 2d c# movement

//This is not a Plataformer type movement 
//Its more of like the old pokemon games type movement if you know what im talking about
//Body Type on RigidBody 2D must be set to kinematic
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D body;

    float horizontal;
    float vertical;

    public float runSpeed = 10.0f;


    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    }
}
Posted by: Guest on June-17-2021
2

how to make character move unity

float horizontal = Input.GetAxis("Horizontal");
 float vertical = Input.GetAxis("Vertical");
 float speed = 5.0f;
 
 void Update(){
     transform.position = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
 }
Posted by: Guest on March-06-2020
-2

unity 2d movement

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

public class PlayerMovement : MonoBehaviour
{
	bool isGrounded = true;
    RigidBody2D rb;
    
    
    private void Start() 
    {
    	rb = GetComponent<RigidBody2D>();
    }
    
    private void FixedUpdate()
    {
    	if(isGrounded && Input.GetButtonDown("Jump")) 
        {
        	rb.AddForce(new Vector2(0, 200));
			IsGrounded = false;
        }
        
        isGrounded = true;
    }	
}
Posted by: Guest on August-21-2021

C# Answers by Framework

Browse Popular Code Answers by Language