Moving simultaneously with UP LEFT, W D using GetAxisRaw. Rotating localScale to flip animation
Hi, this is currently my script. I'm able to move my guy in any direction with the animation going from the "Walk" to "Idle" state. I feel this GetAxisRaw might cause alot of problems when it comes to adding more animations to move him up facing up or down facing down.. in the future. I can't for the life of me figure out how to flip its local scale and continue the walk cycle after moving right to left so that the animation is facing in the opposing direction.
I'm trying to get the character movement of Stardew valley. I have three frames at the moment, Idle and two walk animations, and seems to be working my my favour so far but it looks and feels like a mess.
Here is the script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Animator anim;
void Awake() {
anim = GetComponent<Animator> ();
}
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f )
{
transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
anim.SetBool ("Walk", true);
} else if (Input.GetAxisRaw("Horizontal") == 0.0f || Input.GetAxisRaw("Horizontal") == -0.0f )
anim.SetBool ("Walk", false);
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
anim.SetBool ("Walk", true);
}
}
}
Answer by wnfakind · Mar 26, 2017 at 01:13 AM
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Vector3 theScale;
public float moveSpeed;
private Animator anim;
void Awake() {
anim = GetComponent<Animator> ();
}
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Horizontal") > 0.5f)
{
Vector3 temp = transform.localScale;
temp.x = 0.3f;
transform.localScale = temp;
transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
anim.SetBool ("Walk", true);
} else if (Input.GetAxisRaw("Horizontal") == 0.0f)
anim.SetBool ("Walk", false);
if (Input.GetAxisRaw("Horizontal") < -0.5f)
{
Vector3 temp = transform.localScale;
temp.x = -0.3f;
transform.localScale = temp;
transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
anim.SetBool ("Walk", true);
} else if (Input.GetAxisRaw("Horizontal") == 0.0f)
anim.SetBool ("Walk", false);
}
}
EVERYTHING HORIZONTAL WORKS AS I WANT Unable to copy and paste and change horizontal to vertical values to make it work! I believe this is a mess, but it's slowly work!
EDIT: While copy and pasting code and changing those values, the animation doesn't work anymore horizontally but does move. Vertical keys now move him Horizontal with the animations working.. it broke it.
I'm now trying to split each direction up so I can add separate functions with them. This is going well so far horizontally but when I copy the code and paste it and change Horizontal to Vertical everything goes down the drain. I want to be able to control each one seperatly using GetAxisRaw.
See I figured I'd be able to copy what I have paste it and change everything to vertical but this has no effect besides everything not working. I don't think I actually understand the values floats ect and how they work with the (Input.GetAxisRaw("Horizontal") > 0.5f) <-----
Your answer
Follow this Question
Related Questions
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers
Reduce Player Movement in Air 0 Answers
Player only moves when the key is pressed, not when it's held. 0 Answers
Dash Movement Platformer 0 Answers
Object rotates when moving sideways 0 Answers