hello guys ,
i was following a tutorial , when i ended the tutorial i decided to do it again without looking at the tutorial as a kind of practice and to see if i understood everything .
Now I arrived to the part where my player has a sword , everything is animated and etc …
with the sword there is a little icon swing (for the swing effect) that becomes active whenever the player hits a particular button so it can attack the enemy and the swing becomes active in the scene for spit of seconds (this is done through the animation) but when i wrote the code this is what i wrote
public float movespeed;
private Rigidbody2D myrigidbody;
bool iswalking;
private Vector2 lastmovement;
private Animator anim;
private bool attack;
public float attacktime;
private float attacktimecounter;
// Use this for initialization
void Start () {
myrigidbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
attacktimecounter = attacktime;
}
// Update is called once per frame
void Update () {
iswalking = false;
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f)
{
myrigidbody.velocity = new Vector2 (Input.GetAxisRaw("Horizontal") * movespeed , myrigidbody.velocity.y);
lastmovement = new Vector2 (Input.GetAxisRaw("Horizontal"),0f);
iswalking = true;
}
if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw ("Vertical") < -0.5f)
{
myrigidbody.velocity = new Vector2 (myrigidbody.velocity.x, Input.GetAxisRaw("Vertical") * movespeed);
lastmovement = new Vector2 (0f,Input.GetAxisRaw("Vertical"));
iswalking = true;
}
if (Input.GetAxisRaw ("Horizontal") < 0.5f && Input.GetAxisRaw ("Horizontal") > -0.5f)
{
myrigidbody.velocity = new Vector2 (0f, myrigidbody.velocity.y);
}
if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw ("Vertical") > -0.5f)
{
myrigidbody.velocity = new Vector2 (myrigidbody.velocity.x,0f);
}
if (Input.GetKeyDown (KeyCode.Y))
{
attack = true;
myrigidbody.velocity = Vector2.zero;
attacktimecounter -= Time.deltaTime;
if(attacktimecounter <= 0)
{
attack = false;
attacktimecounter = attacktime;
}
}
anim.SetBool ("iswalking", iswalking);
anim.SetFloat ("lastmovex", lastmovement.x);
anim.SetFloat ("lastmovey", lastmovement.y);
anim.SetFloat ("movex", Input.GetAxisRaw ("Horizontal"));
anim.SetFloat ("movey", Input.GetAxisRaw ("Vertical"));
anim.SetBool ("attack", attack);
}
and this was what the guy in the tutorial wrote:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float movespeed;
private Rigidbody2D myrigidbody;
bool iswalking;
private Vector2 lastmovement;
private Animator anim;
private bool attack;
public float attacktime;
private float attacktimecounter;
// Use this for initialization
void Start () {
myrigidbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
iswalking = false;
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f)
{
myrigidbody.velocity = new Vector2 (Input.GetAxisRaw("Horizontal") * movespeed , myrigidbody.velocity.y);
lastmovement = new Vector2 (Input.GetAxisRaw("Horizontal"),0f);
iswalking = true;
}
if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw ("Vertical") < -0.5f)
{
myrigidbody.velocity = new Vector2 (myrigidbody.velocity.x, Input.GetAxisRaw("Vertical") * movespeed);
lastmovement = new Vector2 (0f,Input.GetAxisRaw("Vertical"));
iswalking = true;
}
if (Input.GetAxisRaw ("Horizontal") < 0.5f && Input.GetAxisRaw ("Horizontal") > -0.5f)
{
myrigidbody.velocity = new Vector2 (0f, myrigidbody.velocity.y);
}
if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw ("Vertical") > -0.5f)
{
myrigidbody.velocity = new Vector2 (myrigidbody.velocity.x,0f);
}
if (Input.GetKeyDown (KeyCode.Y))
{
attacktimecounter = attacktime;
attack = true;
myrigidbody.velocity = Vector2.zero;
anim.SetBool ("attack", true);
/*attacktimecounter -= Time.deltaTime;
if(attacktimecounter <= 0)
{
attack = false;
attacktimecounter = attacktime;
}*/
}
if (attacktimecounter > 0)
attacktimecounter -= Time.deltaTime;
if (attacktimecounter <= 0) {
attack = false;
anim.SetBool ("attack", false);
}
anim.SetBool ("iswalking", iswalking);
anim.SetFloat ("lastmovex", lastmovement.x);
anim.SetFloat ("lastmovey", lastmovement.y);
anim.SetFloat ("movex", Input.GetAxisRaw ("Horizontal"));
anim.SetFloat ("movey", Input.GetAxisRaw ("Vertical"));
}
}
so why mine isn’t working , i feel like i did logic things though
thank you in advance