Question by
Jotenonsos · Apr 05, 2016 at 02:43 PM ·
2d game2d-gameplay
How do I make 2d movement with strafing?
I'm making a top-down 2d game, where I want the player to be able to strafe/move diagonally. Currently it is kinda working, but not as I want it.
Explanation: When player presses Up, the character moves up. If the player simultaneously presses left, the character moves northwest, facing upwards.
This is what I want to happen, except that I also want this to happen:
Player presses left+up. Character moves northwest, facing left. How can I do so?
Here's my current movement script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : 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 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if(movement_vector != Vector2.zero){
anim.SetBool("iswalking", true);
anim.SetFloat ("input_x", movement_vector.x);
anim.SetFloat ("input_y", movement_vector.y);
}else{
anim.SetBool ("iswalking", false);
}
rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
}
}
And here's the animator:
unityask-animator.png
(95.9 kB)
Comment