Reputation: 95
I'm trying to make a Pokémon game in unity2d. I manage to make the grid movement but I don't have a clue on how face the direction without moving (staying in the same place and when A or W or S or D is pressed once just facing the direction without moving).
That's what I have so far:
[SerializeField]
float walkingVelocity = 2;
[SerializeField]
float runingVelocity = 4;
Vector3 p; // For movement
Animator anim;
Vector2 input;
float actualSpeed = 0;
void Start()
{
anim = GetComponent<Animator>();
p = transform.position; // Take the initial position
}
void FixedUpdate()
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
actualSpeed = Input.GetKey(KeyCode.LeftShift) ? walkingVelocity : runingVelocity;
if (input != Vector2.zero && p == transform.position)
{
//CalcularHierbaAlta();
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
if (input.x > 0)
{
//direccion = Direccion.Este;
//PuedeMoverse = CalcularFrente();
p += Vector3.right;
anim.SetFloat("input_x", input.x);
anim.SetFloat("input_y", input.y);
anim.SetBool("isMoving", true);
}
else
{
p -= Vector3.right;
anim.SetFloat("input_x", input.x);
anim.SetFloat("input_y", input.y);
anim.SetBool("isMoving", true);
}
}
else
{
if (input.y > 0)
{
p += Vector3.up;
anim.SetFloat("input_x", input.x);
anim.SetFloat("input_y", input.y);
anim.SetBool("isMoving", true);
}
else
{
p -= Vector3.up;
anim.SetFloat("input_x", input.x);
anim.SetFloat("input_y", input.y);
anim.SetBool("isMoving", true);
}
}
}else if (input == Vector2.zero)
{
anim.SetBool("isMoving", false);
}
transform.position = Vector3.MoveTowards(transform.position, p, actualSpeed * Time.deltaTime);
}
Thank you very much!
Upvotes: 0
Views: 818
Reputation: 139
i had the exact same problem and this is how i managed to do it ;) the line
if (pressWalkTime >= 8) {
can be used to determine how many frames the button can be pressed for, before start moving in the direction.
if (horizontal != 0 || vertical != 0) {
if (pressWalkTime != 0 || !direction.Equals (new Vector2 (horizontal, vertical))) {
pressWalkTime++;
if (pressWalkTime >= 8) {
pressWalkTime = 0;
}
} else {
pressWalkTime = 0;
}
direction = new Vector2 (horizontal, vertical);
if (pressWalkTime == 0 && base.AttemptMove (horizontal, vertical)) {
position += new Vector2 (horizontal, vertical);
}
if (horizontal == 1)
animator.Play ("Walk-Right");
if (horizontal == -1)
animator.Play ("Walk-Left");
if (vertical == 1)
animator.Play ("Walk-Up");
if (vertical == -1)
animator.Play ("Walk-Down");
} else
pressWalkTime = 0;
}
you also should notice that this could does not handle turns. that means that if you are already walking in a direction and then turn in another one you have to wait 8 frames again to start moving. this problem will be solved tommorow, but i'm sure that you can do it on your own ;)
pressWalkTime is an attribute of the class this logic is written in. and is initialized with 0.
Upvotes: 2
Reputation: 299
Check if you are already facing a direction. If not, then rotate your object. If you are already facing the direction that the key press would move you in, then move the player/object.
Upvotes: 1