I am new to Unity, and new to this forum so please go easy on me
I am currently building a 2d game, and have used Sprite Factory which is an awesome tool!
The game is functioning, however, I think I have the wrong idea with regards to the attacks. The animation plays, but thats all it does. It does not attack the enemy, or anything?
Any help would be mostly appreciate on what to do.
using UnityEngine;
using System.Collections;
using FactorySprite = SpriteFactory.Sprite;
public class Attack : MonoBehaviour {
// you forgot to set name of variable representing your sprite
private FactorySprite sprite;
// Use this for initialization
void Start () {
sprite = GetComponent<FactorySprite> (); // Edited
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.A))
{
sprite.Play("Attack");
Vector3 pos = transform.position;
pos.x += Time.deltaTime * 1;
transform.position = pos;
}
}
}
I don’t see any code on attacking. All you tell the player to do is to play the attack animation and move forwards slightly. An animation is simply visual - you will need to write the logic of what actually happens when you attack (damage an enemy, break a box, do whatever it is your game should do).
I’ve never used spritefactory, but In Update() when you press A, your only playing the animation like zaladur stated. From there you could go quite a few routes. You could check the distance between your GO and the enemy GO, and if the distance is less than say 1meter (hypothetically) than deal damage to the enemy (by having say an int in the script attached to the enemy to represent health, and than reducing that int by a value of your damage, if this health int <=0, play a death animation on the enemy or just destroy their GO, possibly adding experience or a kill score to the player). Although you may also want to check what direction your character is facing, so that you can’t kill an enemy whos behind you (your attack would miss visually, but if they’re still within that distance, they still get damage). I would like to help more, but I’m not familiar with sprite factory, and you havent shown us much code besides you just playing an animation from user input. more info = more advice.