2d movement unity
C#
//player must have a rigidbody2D and a box colider
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, 0);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, 0);
}
}
}
Also in C#:
- Title
- change sprite of gameobject unity
- Category
- C#
- Title
- how do I attach a player with a navMeshAgent
- Category
- C#
- Title
- unity t-flip flop
- Category
- C#
- Title
- unity serializefield
- Category
- C#
- Title
- override Microsoft.AspNetCore.Authorization.AuthorizeAttribute
- Category
- C#
- Title
- c sharp create dictionary
- Category
- C#
- Title
- unity get all by tag
- Category
- C#
- Title
- math class C# exponents
- Category
- C#
- Title
- how to make % posibility to spawn an object C# in unity
- Category
- C#
- Title
- c# Authorization has been denied for this request
- Category
- C#
- Title
- built in methods to order a list c#
- Category
- C#
- Title
- Unity3d GPS code
- Category
- C#
- Title
- swith c#
- Category
- C#
- Title
- constructor in inherited class c#
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- downlaod file and use C#
- Category
- C#
- Title
- whats a string
- Category
- C#
- Title
- c# ?
- Category
- C#
- Title
- unity custom update
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- c# window instantly close
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- what is a return statement C#
- Category
- C#
- Title
- how to make error sound c#
- Category
- C#
- Title
- check which activity in focus in android
- Category
- C#
- Title
- sum of digits in c#
- Category
- C#
- Title
- .net core copy file in folder to root
- Category
- C#
- Title
- check if multiple variables are null c#
- Category
- C#
- Title
- unity change text color
- Category
- C#
- Title
- bubble sort c#
- Category
- C#
- Title
- how to draw a dot in c#
- Category
- C#
- Title
- xamarin forms alarm
- Category
- C#
- Title
- c# how to open file explorer
- Category
- C#
- Title
- 2D follow ia unity 2D with agrorange
- Category
- C#
- Title
- or in unity c#
- Category
- C#
- Title
- convert string to double c#
- Category
- C#
- Title
- asp net mvc 5 return view from another controller
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- C# check many strings quickly
- Category
- C#
- Title
- matplotlib measure the width of text
- Category
- C#
- Title
- how to reduce garbage collection c#
- Category
- C#
- Title
- asp.net core mvc not triggering client side validation
- Category
- C#
- Title
- check for collision unity c#
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- onmouseover unity
- Category
- C#
- Title
- wpf use enum description
- Category
- C#
- Title
- how to create more accurate searching c#
- Category
- C#
- Title
- get all child gameObject of gameObject C#
- Category
- C#
- Title
- rotate to face direction
- Category
- C#
- Title
- iphone
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- c# double value with 2 decimal places
- Category
- C#
- Title
- c# ramdom
- Category
- C#
- Title
- unity gameobject.find
- Category
- C#
- Title
- how to stream video from vlc in c#
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- for each textbox c#
- Category
- C#
- Title
- c# variable
- Category
- C#
- Title
- unity get data from firebase
- Category
- C#
- Title
- unity lerp
- Category
- C#
- Title
- unity instantiate
- Category
- C#
- Title
- ionic build
- Category
- C#
- Title
- c# start process
- Category
- C#
- Title
- c# get date without time
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- convert int to double c#
- Category
- C#
- Title
- unity3d find y position on navmesh
- Category
- C#
- Title
- How to get the world position of the edge of an object?
- Category
- C#
- Title
- c# listview
- Category
- C#
- Title
- set decimal point c#
- Category
- C#
- Title
- get day month year from date c#
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- c# push numbers to array
- Category
- C#
- Title
- unity change tag in script
- Category
- C#
- Title
- vector between two points unity
- Category
- C#
- Title
- convert word files to plain text c#
- Category
- C#
- Title
- c# AllowSynchronousIO to true
- Category
- C#
- Title
- question mark operator in c#
- Category
- C#
- Title
- unity get all children
- Category
- C#
- Title
- for each property in object c#
- Category
- C#
- Title
- c# read excel file
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- c# list tuple
- Category
- C#
- Title
- how to stop a form c#
- Category
- C#
- Title
- font family behind code uwp c#
- Category
- C#
- Title
- datagridview column color c#
- Category
- C#
- Title
- c# to binary
- Category
- C#
- Title
- c# list length
- Category
- C#
- Title
- c sharp tryparse
- Category
- C#
- Title
- lat long data type c#
- Category
- C#
- Title
- c# inotifypropertychanged best practices
- Category
- C#
- Title
- transform.Translate movement
- Category
- C#
- Title
- c# get foreground window
- Category
- C#
- Title
- float to int c#
- Category
- C#
- Title
- animations for pause menu
- Category
- C#
- Title
- stop unity editor playing
- Category
- C#
- Title
- c# aspx return image
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- how to clear console in c#
- Category
- C#
- Title
- unity to string
- Category
- C#