Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New Member - Am I going the wrong way about Attack Scripting?

Discussion in 'Scripting' started by raziel786, May 9, 2014.

  1. raziel786

    raziel786

    Joined:
    May 6, 2014
    Posts:
    22
    Hi =)

    I am new to Unity, and new to this forum so please go easy on me :p

    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.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using FactorySprite = SpriteFactory.Sprite;
    4. public class Attack : MonoBehaviour {
    5.    
    6.     // you forgot to set name of variable representing your sprite
    7.     private FactorySprite sprite;
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.         sprite = GetComponent<FactorySprite> (); // Edited
    12.     }
    13.     void Update ()
    14.     {
    15.         if(Input.GetKeyDown(KeyCode.A))
    16.         {
    17.             sprite.Play("Attack");
    18.             Vector3 pos = transform.position;
    19.             pos.x += Time.deltaTime * 1;
    20.             transform.position = pos;
    21.         }
    22.     }
    23. }
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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).
     
  3. raziel786

    raziel786

    Joined:
    May 6, 2014
    Posts:
    22

    Hi,

    Thank you for your reply. Knew I was off with what I was doing.

    Any sample codes I can work with with regards to getting the chararacter to attack the enemy?

    Regards
     
  4. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    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.
     
  5. Bionicle_fanatic

    Bionicle_fanatic

    Joined:
    Jun 8, 2013
    Posts:
    368
    BTW, you don't seem to have allowed PMs on these forums, so I can't contact you about the AI scripting job [;)] [:p]