unity 2d platformer movement script c#
C#
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//Movement
public float speed;
public float jump;
float moveVelocity;
//Grounded Vars
bool grounded = true;
void Update ()
{
//Jumping
if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W))
{
if(grounded)
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
}
}
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
}
//Check if Grounded
void OnTriggerEnter2D()
{
grounded = true;
}
void OnTriggerExit2D()
{
grounded = false;
}
}
Also in C#:
- Title
- c# double question mark
- Category
- C#
- Title
- how to detected WindowCloseEvent in other window wpf
- Category
- C#
- Title
- unity if gameobject exists
- Category
- C#
- Title
- button commandfield commandargument pass textbox
- Category
- C#
- Title
- linq c# or
- Category
- C#
- Title
- void start
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- how to add a list to observablecollection in c#
- Category
- C#
- Title
- arry in c#
- Category
- C#
- Title
- how to make a datatable in c#
- Category
- C#
- Title
- initialize matrix c#
- Category
- C#
- Title
- c# random string
- Category
- C#
- Title
- trello
- Category
- C#
- Title
- c# round number down
- Category
- C#
- Title
- start a particle effect when a button is pushed
- Category
- C#
- Title
- c# random number between 0 and 1
- Category
- C#
- Title
- dynamically add rows to datagridview c#
- Category
- C#
- Title
- how to locate a specific element in a list c#
- Category
- C#
- Title
- razor: show editable list
- Category
- C#
- Title
- internal c#
- Category
- C#
- Title
- condition when a animation finishes in unity
- Category
- C#
- Title
- vb.net tostring numeric format string
- Category
- C#
- Title
- c# inheritance constructor
- Category
- C#
- Title
- c# convert Unix time in seconds to datetime
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- linq c# where condition
- Category
- C#
- Title
- 5 second timer in c#
- Category
- C#
- Title
- how to reload app.config file at runtime in c#
- Category
- C#
- Title
- c# make http request
- Category
- C#
- Title
- override Microsoft.AspNetCore.Authorization.AuthorizeAttribute
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- built in methods to order a list c#
- Category
- C#
- Title
- c# list to string join
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- c# reverse a string for loop
- Category
- C#
- Title
- c# input integer
- Category
- C#
- Title
- unity material offset script
- Category
- C#
- Title
- what is type unity
- Category
- C#
- Title
- jump script unity 2d
- Category
- C#
- Title
- how to deactivate objects through scripts in unity
- Category
- C#
- Title
- c# remove first 5 characters from string
- Category
- C#
- Title
- multidimensional meaning
- Category
- C#
- Title
- http post request login example asp.net c#
- Category
- C#
- Title
- convert number of days into months c#
- Category
- C#
- Title
- c# resize image keep aspect ratio
- Category
- C#
- Title
- try catch c#
- Category
- C#
- Title
- c# change label forecolor code
- Category
- C#
- Title
- how to create and trigger a function unity animation events
- Category
- C#
- Title
- html hidden text
- Category
- C#
- Title
- c# compile into an exe
- Category
- C#
- Title
- how to create more accurate searching c#
- Category
- C#
- Title
- c# find where word is contained in a string
- Category
- C#
- Title
- foreach c#
- Category
- C#
- Title
- isGrounded script for copy
- Category
- C#
- Title
- c# asp.net hover tooltip
- Category
- C#
- Title
- vector3.lerp
- Category
- C#
- Title
- findobject getcomponent
- Category
- C#
- Title
- bitmap to byte array c#
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- simple reset transform.rotation c#
- Category
- C#
- Title
- get absolute url c#
- Category
- C#
- Title
- c# string remove special characters
- Category
- C#
- Title
- debug.log
- Category
- C#
- Title
- c# how to refreshyour bindingsource
- Category
- C#
- Title
- c# how to have a multiline console.writeline
- Category
- C#
- Title
- how to start grid from where the data starts in c# charts
- Category
- C#
- Title
- autofixture ignore property
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- format float to time c#
- Category
- C#
- Title
- unity scene name get
- Category
- C#
- Title
- c# input
- Category
- C#
- Title
- unity monobehaviour
- Category
- C#
- Title
- c# aspx return image
- Category
- C#
- Title
- linq query select top 1 c#
- Category
- C#
- Title
- unity how to change rotation
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- how to trim path in C#
- Category
- C#
- Title
- c# remove last character from string
- Category
- C#
- Title
- radians to degree c#
- Category
- C#
- Title
- unity quaternion
- Category
- C#
- Title
- asp.net core task iactionresult
- Category
- C#
- Title
- c# getasync response
- Category
- C#
- Title
- C# downloadstirng download old
- Category
- C#
- Title
- MVC company assignments
- Category
- C#
- Title
- C# .net core convert to int round up
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#
- Title
- rotatearound unity
- Category
- C#
- Title
- c# anonymous class
- Category
- C#
- Title
- c# iterate over string
- Category
- C#
- Title
- how to cast list to observablecollection c#
- Category
- C#
- Title
- c# authorize attribute
- Category
- C#
- Title
- unity c# set gameobject active
- Category
- C#
- Title
- c# string to memorystream
- Category
- C#
- Title
- same click event diffrenet buttonms c#
- Category
- C#
- Title
- vector between two points unity
- Category
- C#
- Title
- unity cast float to int
- Category
- C#
- Title
- serilog loglevel order
- Category
- C#
- Title
- write last line txt file c#
- Category
- C#
- Title
- csv to dataset c#
- Category
- C#