TopDown IfStatment Help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterContoller : MonoBehaviour
{
public float Speed = 0.1f;
Transform Player;
void Start ()
{
Player = GameObject.Find("Player").GetComponent<Transform> ();
}
void FixedUpdate ()
{
Vector3 Movement = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
Movement = Vector3.up * Speed;
}
if (Input.GetKey(KeyCode.A))
{
Movement = Vector3.left * Speed;
}
if (Input.GetKey(KeyCode.S))
{
Movement = Vector3.down * Speed;
}
if (Input.GetKey(KeyCode.D))
{
Movement = Vector3.right * Speed;
}
Player.Translate (Movement);
}
}
The problem that I'm having is that these ifStatments are going in order. So for example, if I'm holding down W I can still press ASD to go in other directions. But if I'm Pressing like S I can only go to the right by pressing D because of the order of the if statements. I want to get it to where they all are able to go in all directions or so you have to release the key that you are holding down to go in a different direction. All Help appreciated <3,
Answer by Skokon · Sep 30, 2018 at 04:30 PM
You have to do it like this way
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterContoller : MonoBehaviour
{
public float Speed = 0.1f;
Transform Player;
void Start ()
{
Player = GameObject.Find("Player").GetComponent<Transform> ();
}
void FixedUpdate ()
{
Vector3 Movement = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
Movement = Vector3.up * Speed;
}
else if (Input.GetKey(KeyCode.A))
{
Movement = Vector3.left * Speed;
}
else if (Input.GetKey(KeyCode.S))
{
Movement = Vector3.down * Speed;
}
else if (Input.GetKey(KeyCode.D))
{
Movement = Vector3.right * Speed;
}
Player.Translate (Movement);
}
}
@Skokon This Change Seems to just reverse the effect. $$anonymous$$aking it so when I am holding down the D $$anonymous$$ey Im able to Press SAW but if I'm holding down the S $$anonymous$$ey I Can Only Press AW.
Assign Horizontal and Vertical input keys from Edit -> Project settings -> Input
and change your code to this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterContoller : $$anonymous$$onoBehaviour
{
float Speed = 0.1f;
Transform Player;
float inputHorizontal;
float inputVertical;
void Start ()
{
Player = GameObject.Find("Player").GetComponent<Transform> ();
}
void FixedUpdate ()
{
Vector3 $$anonymous$$ovement = Vector3.zero;
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
$$anonymous$$ovement = ( Player.right * inputHorizontal + Player.forward * inputVertical).normalized;
Player.Translate ($$anonymous$$ovement);
}
}
https://gyazo.com/ff6a4a548443794c8e56962a5e0e651b
I'm getting some errors. Don't I need to Include "Vector3 $$anonymous$$ovement = Vector3.zero;" ?
Answer by Jan-Eylander · Sep 30, 2018 at 11:36 PM
If you want to stick to your code snippet you can just add a + infront of the = like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterContoller : MonoBehaviour
{
public float Speed = 0.1f;
Transform Player;
void Start ()
{
Player = GameObject.Find("Player").GetComponent<Transform> ();
}
void FixedUpdate ()
{
Vector3 Movement = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
Movement += Vector3.up * Speed;
}
if (Input.GetKey(KeyCode.A))
{
Movement += Vector3.left * Speed;
}
if (Input.GetKey(KeyCode.S))
{
Movement += Vector3.down * Speed;
}
if (Input.GetKey(KeyCode.D))
{
Movement += Vector3.right * Speed;
}
Player.Translate (Movement);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612175521im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make an object move, not teleport? 0 Answers
How can I stop my character from walking during the attack animation? 1 Answer
Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers
Animator: multiple characters and one script 0 Answers
How to use hold/tap interactions in new Input System 0 Answers