Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity topdown movement

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

top down movement unity

Rigidbody2D body;

float horizontal;
float vertical;
float moveLimiter = 0.7f;

public float runSpeed = 20.0f;

void Start ()
{
   body = GetComponent<Rigidbody2D>();
}

void Update()
{
   // Gives a value between -1 and 1
   horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
   vertical = Input.GetAxisRaw("Vertical"); // -1 is down
}

void FixedUpdate()
{
   if (horizontal != 0 && vertical != 0) // Check for diagonal movement
   {
      // limit movement speed diagonally, so you move at 70% speed
      horizontal *= moveLimiter;
      vertical *= moveLimiter;
   } 

   body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity 2d movement 
Csharp :: c# run loop x times 
Csharp :: unity transparent object 
Csharp :: c# compress string 
Csharp :: c# find process by name 
Csharp :: clear controls from panel c# 
Csharp :: cannot convert string to generic type c# 
Csharp :: c# socket connect timeout 
Csharp :: get child of transform by index unity 
Csharp :: razor confirm password validation 
Csharp :: = in c# 
Csharp :: c# shuffle 
Csharp :: unity c# debug.log 
Csharp :: escape double quotes c# 
Csharp :: .net c# print object 
Csharp :: unity random number 
Csharp :: httpclient post c# example 
Csharp :: get type of variable c# 
Csharp :: change name of gameobject 
Csharp :: unity post processing ui 2d 
Csharp :: contains c# 
Csharp :: c# named parameters 
Csharp :: how return only value of array in laravel 
Csharp :: c# run cmd hidden 
Csharp :: c# thread 
Csharp :: unity instantiate prefab 
Csharp :: count number of properties on an object C# 
Csharp :: c# datagridview change selected row color 
Csharp :: how to save a dictionary as a csv file in c# 
Csharp :: asp.net textarea disable resize 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =