2d character controller unity
C#
public float speed;
public float jumpForce;
public float moveInput;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJump;
public int ejV;
private bool isGrounded;
private bool facingRight = true;
private Rigidbody2D rb;
void Start()
{
extraJump = ejV;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (isGrounded == true)
{
extraJump = ejV;
}
if (Input.GetKey(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}else if (Input.GetKey(KeyCode.Space) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}public float speed;
public float jumpForce;
public float moveInput;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJump;
public int ejV;
private bool isGrounded;
private bool facingRight = true;
private Rigidbody2D rb;
void Start()
{
extraJump = ejV;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (isGrounded == true)
{
extraJump = ejV;
}
if (Input.GetKey(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}else if (Input.GetKey(KeyCode.Space) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
Also in C#:
- Title
- how to see a tag of what you hit with raycast
- Category
- C#
- Title
- c# ref
- Category
- C#
- Title
- c# window instantly close
- Category
- C#
- Title
- mvc c# w3schools
- Category
- C#
- Title
- native-googlesignin configuration is null!?
- Category
- C#
- Title
- unity set object scale
- Category
- C#
- Title
- asp list box
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- csharp datetime string format
- Category
- C#
- Title
- center an image horizontally and vertically
- Category
- C#
- Title
- decalre an int list mvc
- Category
- C#
- Title
- c# read a webpage data
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- how to add a componet to a gameobject throgh code unity
- Category
- C#
- Title
- create new object from generic c#
- Category
- C#
- Title
- unity remove parent
- Category
- C#
- Title
- usermanager find based on role
- Category
- C#
- Title
- unity transfoprm position y change
- Category
- C#
- Title
- how to set a gizmo color unity
- Category
- C#
- Title
- c# throw new exception
- Category
- C#
- Title
- c# adding to a list
- Category
- C#
- Title
- find mongodb c# with task T
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- unity how to change max fps
- Category
- C#
- Title
- vb.net check if datatable has rows
- Category
- C#
- Title
- c# linq to get most recent item from IList
- Category
- C#
- Title
- c# datatable copy selected rows to another table
- Category
- C#
- Title
- if get key down unity
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- access dic by key c#
- Category
- C#
- Title
- eager loading c#
- Category
- C#
- Title
- get setter c# model
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#
- Title
- set object to random color unity
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- check for collision unity c#
- Category
- C#
- Title
- c# clear console read chache
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- convert string to datetime c#
- Category
- C#
- Title
- How do i destroy a prefab without the error?
- Category
- C#
- Title
- C# .net core convert int to enum
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- initialize matrix c#
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- what is a return statement C#
- Category
- C#
- Title
- c# region tag
- Category
- C#
- Title
- c#if
- Category
- C#
- Title
- c# else if
- Category
- C#
- Title
- Unity if object doens't exist
- Category
- C#
- Title
- get set c#
- Category
- C#
- Title
- how to add to a list c#
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- font family behind code uwp c#
- Category
- C#
- Title
- c# how to print
- Category
- C#
- Title
- c# find index element array
- Category
- C#
- Title
- unity load text resources from subfolder
- Category
- C#
- Title
- send type as argument c#
- Category
- C#
- Title
- dont destroy on load unity
- Category
- C#
- Title
- c# get list of all class fields
- Category
- C#
- Title
- c# date
- Category
- C#
- Title
- unity movetowards 2d
- Category
- C#
- Title
- unity access child
- Category
- C#
- Title
- c# class declaration
- Category
- C#
- Title
- how to delete transition unity
- Category
- C#
- Title
- c# how to have a multiline console.writeline
- Category
- C#
- Title
- unity multiply xyz of two vectors
- Category
- C#
- Title
- c# empty IEnumerable
- Category
- C#
- Title
- json stringify c#
- Category
- C#
- Title
- iterate through dictionary c#
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- drag object unity 2d
- Category
- C#
- Title
- player script unity
- Category
- C#
- Title
- delay in unity
- Category
- C#
- Title
- how to trim path in C#
- Category
- C#
- Title
- c# get foreground window
- Category
- C#
- Title
- unity jump
- Category
- C#
- Title
- can't add an editor script
- Category
- C#
- Title
- download file from url asp net web api c#
- Category
- C#
- Title
- console application in c# ms crm
- Category
- C#
- Title
- fahrenheit to celsius c#
- Category
- C#
- Title
- how to route back to url using Request.Headers["Referer"].ToString() in asp.net core
- Category
- C#
- Title
- c# read excel file
- Category
- C#
- Title
- using serial port in c#
- Category
- C#
- Title
- c# tostring mmm dd yyyy
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- unity cast float to int
- Category
- C#
- Title
- asking for user input integer c#
- Category
- C#
- Title
- entity framework update child records
- Category
- C#
- Title
- list.addrange in c#
- Category
- C#
- Title
- call Textboxfor in cs
- Category
- C#
- Title
- Movement 2d unity
- Category
- C#
- Title
- c# change cursor
- Category
- C#
- Title
- c# array isn't working
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- orderby make sunday last day c#
- Category
- C#
- Title
- c# main method
- Category
- C#
- Title
- how to reference the position of a game object unity
- Category
- C#
- Title
- list c#
- Category
- C#