2d Movement Unity Code Example


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

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable