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 :: c# get command line arguments 
Csharp :: c# unity detect any keyboard input 
Csharp :: asp.net core 3.1 ajax partial view 
Csharp :: c# string to byte[] 
Csharp :: unity time deltatime 
Csharp :: how to set rigidbody velocity in unity 
Csharp :: how to instantiate a gameobject 
Csharp :: c# get datatable column names to list 
Csharp :: c# byte array to file 
Csharp :: google sheet script change text color 
Csharp :: change button color in script unity 
Csharp :: unity icons 
Csharp :: c# onmousedown. unity 
Csharp :: move files from one directory to another using c# 
Csharp :: movetowards unity 
Csharp :: unity post processing ui 2d 
Csharp :: get last character of string c# 
Csharp :: c# adding to a list 
Csharp :: getcomponent unity 
Csharp :: unity call function on animation finish 
Csharp :: color unity 
Csharp :: C# function return datareader 
Csharp :: difference between class and struct in c# 
Csharp :: roman to number 
Csharp :: dns ttl meaning 
Csharp :: multithreading in c# 
Csharp :: list search c# 
Csharp :: c# linq distinct group by nested list 
Csharp :: get color of pixel c# 
Csharp :: generate certificate in windows 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =