ive been fiddling around for weeks now trying everything i can think of to make my controls work from keyboard input to gui input but i havent to get them to function right so im going to go back to square 1 and reach out and see if anyone can lead me in the right direction
i have these 2 scripts for controlling my player via keyboard input
this is for attacking an enemy
using UnityEngine;
using System.Collections;
public class attacks : MonoBehaviour {
public int speed = 2;
private int attack = 0;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
attack = 0;
speed = 0;
if(Input.GetKeyDown(KeyCode.Q))
{
attack = 1;
}
if(Input.GetKeyDown(KeyCode.W))
{
attack = 2;
}
if(Input.GetKeyDown(KeyCode.E))
{
attack = 3;
}
if(Input.GetKeyDown(KeyCode.R))
{
attack = 4;
}
if(Input.GetKey(KeyCode.S))
{
attack = -1;
}
anim.SetInteger("attack", attack);
}
}
each “attack” value corresponds to an animation parameter to call an animation
this script is for moving left and right
using UnityEngine;
using System.Collections;
public class playermovement : MonoBehaviour
{
public int speed = 2;
private int direction = 0;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
direction = 0;
if(Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
direction = 1;
}
if(Input.GetKey(KeyCode.A))
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
direction = -1;
}
if(Input.GetKey(KeyCode.D)
&& Input.GetKey(KeyCode.A))
{
direction = 0;
}
anim.SetInteger("speed", direction);
}
}
the “direction” values correspond to left or right direction in the animator
i don’t want this done totally for me, i want to know how to do it and be able to recreate this in the future, but i would appreciate it if someone could give me a general idea of what functions i should be using
by the way, my GUI buttons were made using unity 4.6 with the canvas Button UI feature if that makes a difference on how it should be done