• 10
• 9
• 12
• 14
• 14

# Need Help With My Script And Animator.

This topic is 642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay so I have created a player archer character which when he using the arrow keys,faces those directions and upon hitting space when in that direction,the bow attack animation is played.

The code works fine but there are some nerve-wrecking problems:

when I unpress an arrow key,the character always returns to his default direction and when I press space when no arrow key is pressed,the right bow attack animation is played and also when i press the up arrow key and the space bar,the up bow attack animation does not play and instead again,the right bow attack animation is played.

I have looked at everything again and again but i can't seem to find anything wrong.the animation's assigned are okay and the code is okay too.
Don't know whats wrong. I have been stuck at this problem all day.

I have attached some screenshots so you can see how I have achieved this:

this is the character I am working with>>>screenshot_2

this is my animator>>>>>screenshot_1

this is my blend tree in the idle state>>>>>screenshot_3

and so far,this is my script:

using UnityEngine;
using System.Collections;

public class PlayerDirectionAndShooting : MonoBehaviour {

Rigidbody2D rbody;
Animator anim;

// Use this for initialization
void Start () {

rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}

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

Vector2 direction_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

anim.SetFloat("input_x", direction_vector.x);

anim.SetFloat("input_y", direction_vector.y);

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_x") <= -1)
{
anim.SetBool("is_shooting_left", true);
}

}
else
{
anim.SetBool("is_shooting_left", false);
};

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_y") <= -1)
{
anim.SetBool("is_shooting_down", true);
}

}
else
{
anim.SetBool("is_shooting_down", false);
};

if (Input.GetKeyDown("space"))
{

if (anim.GetFloat("input_y") <= 1)
{
anim.SetBool("is_shooting_up", true);
}

}
else
{
anim.SetBool("is_shooting_up", false);
};

if (Input.GetKeyDown("space"))
{

if (anim.GetFloat("input_x") <= 1)
{
anim.SetBool("is_shooting_right", true);
}

}
else
{
anim.SetBool("is_shooting_right",false);
};

}

}

##### Share on other sites

I'd say take a look at this line:

Vector2 direction_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));


you are always creating a new vector there which is fine but what does Input.getAxisRaw() return when there was no input. what's the default?

Your problem seems to be that it always uses some default if you don't have any input. You should take a look at these defaults and think of a way of ignoring them when there wasn't.

Pseudo code should be smthn like this:

if(input) update_values();

else take_previous_values()

##### Share on other sites

As the other above had mentioned. You keep destroying what data you previously had, when you should be caching it. So every time that object's update is overwith, your old data keeps falling out of scope, and needs to be made with a new one.

What you should do instead is create a private property that stores your Vector2f's data.

Another thing to be mindful of, is if-block scoping. It's not a problem right now, but I have a feeling this may crop up. Animator is very stateful. Unless it's data is declared on a superior scope, anything that is created in that if check will also be deleted once the if block is over with.