Search Unity

Attack Script

Discussion in 'Scripting' started by 2k-sp, Jul 24, 2014.

  1. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10
    Hi Everyone
    It´s me again
    Can somebody help with this script . I did it after looking a tutorial on youtube . the Problem is
    i can´t add a script which can make my character attack zombie . He just walks from right and left like inside tht tuto but i don´t know how i can make him attack by animation using animator.
    i did a screenshot of my code.
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First of all... screenshot of code? o_O Just paste it.

    Second, your script calls animation.Play. "animation" points to an Animation component, which is not what you have: you have an Animator. (Since Unity has a "legacy" animation system plus Mecanim, the names of these components can get confusing) The script is already creating a reference to the Animator component ("go"), use that. You'll have to set the "attack1" trigger.
     
  3. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10

    thx u but i´m still learning how to programm and what u said i´ve got but i do not really know how to write it
    can u help?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Code (csharp):
    1. go.SetTrigger("attack1");
     
    2k-sp likes this.
  5. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10
    here is the Script i did it like u told me but it didn´t work

    using UnityEngine;
    using System.Collections;

    public class gaby : MonoBehaviour
    {

    public float maxSpeed = 5f;
    bool turnBack = true;
    Animator go;



    // Use this for initialization
    void Start ()
    {
    go = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update ()
    {

    float move = Input.GetAxis("Horizontal");
    go.SetFloat ("speed", Mathf.Abs (move));
    rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    if (move > 0 && !turnBack)
    turningBack ();


    else if (Input.GetKeyDown (KeyCode.Q))

    {
    // i have no Idea how to make my character attack after creating an Animation in animator
    }



    else if (move < 0 && turnBack)
    turningBack ();
    }




    void turningBack()
    {
    turnBack = ! turnBack;
    Vector3 theScale = transform.localScale;
    theScale.x*=-1;
    transform.localScale = theScale;

    }

    }