Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
The modifier 'private' is not valid for this item
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum PlayerState { attack, walk } public class PlayerMovement : MonoBehaviour { public float speed; private Rigidbody2D myRigidbody; private Vector3 change; private Animator animator; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); myRigidbody = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { change = Vector3.zero; change.x = Input.GetAxisRaw("Horizontal"); change.y = Input.GetAxisRaw("Vertical"); if (Input.GetButtonDown("attack") && currentState != PlayerState.attack) { StartCoroutine(AttackCo()); } else if (currentState == PlayerState.walk) { UpdateAnimationAndMove(); } private IEnumerator AttackCo() { animator.SetBool("attacking", true); currentState = PlayerState.attack; yield return null; animator.SetBool("attacking", false); yield return new WaitForSeconds(.33f); currentState = PlayerState.walk } void UpdateAnimationAndMove() { if (change != Vector3.zero) { MoveCharacter(); animator.SetFloat("moveX", change.x); animator.SetFloat("moveY", change.y); animator.SetBool("moving", true); } else { animator.SetBool("moving", false); } } } void MoveCharacter() { myRigidbody.MovePosition( transform.position + change * speed * Time.deltaTime ); } }
Show compiler warnings
[
+
]
Show input
edit mode
|
history
|
discussion