Question by
Enmaboya · Mar 21, 2017 at 07:17 AM ·
c#animation2d animation
How to make the animation of attack? 2D
Tell me, please, what to write in Attack method. And why is attack animation is not working? Point me please to the right path!
My project: https://drive.google.com/open?id=0B27SNSPxMPOKaW1iZkw3WXBxOGM
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour
{
[SerializeField]
private int lives = 5;
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private float jumpForce = 15.0F;
public int score;
private bool isGrounded = false;
private CharState State
{
get { return (CharState)animator.GetInteger("State"); }
set { animator.SetInteger("State", (int)value); }
}
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
if (Input.GetButton ("Fire1")) Attack();
if (isGrounded) State = CharState.Idle;
if (Input.GetButton("Horizontal")) Run();
if (isGrounded && Input.GetButtonDown("Jump")) Jump();
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
if (isGrounded) State = CharState.Run;
}
private void Jump()
{
State = CharState.Jump;
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Attack()
{
State = CharState.Attack;
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
if (!isGrounded) State = CharState.Jump;
}
void OnGUI()
{
GUI.Box(new Rect(0, 0, 100, 100), "Stars:" + score);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "star")
{
Destroy(col.gameObject); score++;
}
if (col.gameObject.name == "dieCollider")
{
Destroy(gameObject); Application.LoadLevel(Application.loadedLevel); ;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "saw")
{
specialeffect.Instance.Explosion(transform.position);
Destroy(gameObject);;
}
if (col.gameObject.name == "EndLevel")
{
if (!(GameObject.FindGameObjectWithTag("star"))) Application.LoadLevel("scene2");
}
}
public enum CharState
{
Idle,
Run,
Jump,
Attack,
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612113625im_/https://answers.unity.com/themes/thub/images/avi.jpg)