"Player movement with animation unity" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "Player movement with animation unity" answered properly. Developers are finding an appropriate answer about Player movement with animation unity related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like Player movement with animation unity. Enter your desired code related query in the search bar and get every piece of information about C# code related question on Player movement with animation unity.
Player movement with animation unity
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
private Animator anim;
private float moveSpeed;
private float dirX;
private bool facingRight = true;
private Vector3 localScale;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
localScale = transform.localScale;
moveSpeed = 3f;
}
private void Update()
{
dirX = Input.GetAxis("Horizontal") * moveSpeed;
if (Input.GetButtonDown("Jump") && rb.velocity.y > -0.1 && rb.velocity.y < 0.1)
rb.AddForce(Vector2.up * 500f);
if (Mathf.Abs(dirX) > 0 && rb.velocity.y > -0.1 && rb.velocity.y < 0.1)
{
anim.SetBool("isRunning", true);
}
else
anim.SetBool("isRunning", false);
if (rb.velocity.y > -0.1 && rb.velocity.y < 0.1)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", false);
}
if (rb.velocity.y > 0.1)
anim.SetBool("isJumping", true);
if (rb.velocity.y < -0.1)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", true);
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
private void LateUpdate()
{
if (dirX > 0)
facingRight = true;
else if (dirX < 0)
facingRight = false;
if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
}
All those coders who are working on the C# based application and are stuck on Player movement with animation unity can get a collection of related answers to their query. Programmers need to enter their query on Player movement with animation unity related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Player movement with animation unity for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of Player movement with animation unity while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "Player movement with animation unity". Visit this developer's friendly online web community, CodeProZone, and get your queries like Player movement with animation unity resolved professionally and stay updated to the latest C# updates.