0
\$\begingroup\$

I'm busy following this YouTube tutorial for a 2D game in Unity by inScope: https://www.youtube.com/watch?v=-gC1u7qHMY4&list=PLX-uZVK_0K_6VXcSajfFbXDXndb6AdBLO&index=6

I decided to not finish the attacking episode and skipped to the next episode. But things were not working out so I deleted all my new work so everything will be back to how it was before.

But low-and-behold my attack stopped working. What should happen is whenever I press Left Shift my character should play his attack animation.

I've rechecked the videos again and again but I can't find whats wrong with my script! The script is in C#, and there are no errors in the script. I don't know whats wrong PLEASE HELP!

public class Player : MonoBehaviour 
{
  private Rigidbody2D myRigidbody;

  private Animator myAnimator; 

  [SerializeField]
  private float movementSpeed;

  private bool attack;

  private bool facingRight;

  // Use this for initialization
  void Start () 
  {
    facingRight = true;
    myRigidbody = GetComponent<Rigidbody2D> ();
    myAnimator = GetComponent<Animator> ();
  }

  void Update()
  {
    HandleInput ();
  }

  // Update is called once per frame
  void FixedUpdate () 
  {
    float horizontal = Input.GetAxis ("Horizontal");

    HandleMovement (horizontal);

    Flip (horizontal);

    HandleAttack ();

    ResetValues ();
  }

  private void HandleMovement(float horizontal)
  {
    myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);  // vector2 = x-axis - 1, y-axis = 0
    myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
  }


  private void HandleAttack()
  {
    if (attack) 
    {
      myAnimator.SetTrigger ("Attack"); 
    }
  }


  private void HandleInput()
  {
    if (Input.GetKeyDown (KeyCode.LeftShift)) 
    {
      attack = true;
    }
  }

  private void Flip(float horizontal)
  {
    if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) 
    {
      facingRight = !facingRight;

      Vector3 theScale = transform.localScale;

      theScale.x *= -1;

      transform.localScale = theScale;
    }
  }

  private void ResetValues()
  {
    attack = false;
  }
}
\$\endgroup\$
5
  • 3
    \$\begingroup\$ A bit offtopic, but this is one of the reasons why one should always use version control system for their projects. It is worth the time/effort put into setting it up. \$\endgroup\$
    – wondra
    Commented Feb 20, 2017 at 11:32
  • 2
    \$\begingroup\$ "stopped working" does not described the issue. What do you expect to happen, and what happens instead? Did you use the debugger to see why/where/how it (did not) happened? Most of the users here will not watch a series of video just to pin-point where you went wrong. Please edit the question and add more details so that we can help you better :) \$\endgroup\$
    – Vaillancourt
    Commented Feb 20, 2017 at 11:49
  • 1
    \$\begingroup\$ I would take a look at the animation controller and see if it handles the "Attack" trigger and if its animations are "wired" correctly to each other. By the way: While the Unity animation system is great for 3d models, I personally find it quite overengineered for most 2d games and usually prefer to handle animations on my own. Especially when I have multiple spritesheets which are all organized the same way. \$\endgroup\$
    – Philipp
    Commented Feb 20, 2017 at 11:52
  • \$\begingroup\$ Are you sure this script is attached to your GameObject? \$\endgroup\$ Commented Feb 20, 2017 at 15:06
  • \$\begingroup\$ @Hamza Hasan I think so. My character can move left and right like the script says. But I cant figure out why he doesn't attack when I tell him to. \$\endgroup\$ Commented Feb 23, 2017 at 9:05

1 Answer 1

0
\$\begingroup\$

As mentioned before "stopped working" does very little to show what is wrong. Maybe check if the attack value is true by putting this code in void update : Debug.Log(attack); and look closely at you console. It will print the value although a lot it will print it and if it remains false well look at editing this part:

private void HandleInput()
{
  if (Input.GetKeyDown (KeyCode.LeftShift)) 
  {
    attack = true;
  }
}

If it does not work I tried but I am also noob. :P

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .