I'm trying to update a glitchy game and fix the movement. I would like to move it using keyboard. I got mouse working. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player : MonoBehaviour { Rigidbody2D rb; public Text _Mouse_Position; public Button Droid_Up; public Button Droid_Down; public Animator Player_Anim; public Text Travel; public float DistanceTraveled; public Text PlayerHealth; public float Yincrement; public float Speed; public float MaxHeight; public float MinHeight; public int Health = 10; public GameObject Particle_Effect; public GameObject PanController; public Button Fly_Again; private Vector2 targetPos; private Vector3 TargetFollow; private void Awake() { rb = GetComponent<Rigidbody2D>(); } void Start() { //Panel if (PanController != null) { PanController.SetActive(false); } //Player Starting Health Health = 10; //Travel DistanceTraveled = 0; //Animations Player_Anim = GetComponent<Animator>(); if (WBG_Config._Build == "Phone") { //Show Buttons Droid_Down.gameObject.SetActive(true); Droid_Up.gameObject.SetActive(true); } else { if (Droid_Down != null) { //Hide Buttons Droid_Down.gameObject.SetActive(false); Droid_Up.gameObject.SetActive(false); } } } private void Update() { try { //Travel DistanceTraveled += 0.0001f; Travel.text = System.Math.Round(DistanceTraveled, 2).ToString() + " Miles"; Game_Data.Total_Distance = DistanceTraveled; //Player Health PlayerHealth.text = Health.ToString(); if (Health <= 0) { //Save Data Game_Data.Total_Distance = DistanceTraveled; //Reload Scene Instantiate(Particle_Effect, transform.position, Quaternion.identity); Health = 0; PlayerHealth.text = Health.ToString(); PanController.SetActive(true); Fly_Again.Select(); Destroy(gameObject); //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } else { //Mouse if(Game_Data._Movement_Type ==0 ) { float x = Positon_X(Input.mousePosition.x); float y = Positon_Y(Input.mousePosition.y); _Mouse_Position.text = "x:" + Input.mousePosition.x.ToString() + " " + "y:" + Input.mousePosition.y.ToString(); TargetFollow = new Vector3(x, y);//Input.mousePosition; TargetFollow = Camera.main.ScreenToWorldPoint(TargetFollow); TargetFollow.z = transform.position.z; transform.position = Vector3.Lerp(transform.position, TargetFollow, Speed * Time.deltaTime); } else if (Game_Data._Movement_Type == 1) { transform.position = Vector2.MoveTowards(transform.position, targetPos, Speed * Time.deltaTime); foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode))) { //Keypress Method if (Input.GetKeyDown(kcode)) { Vector3 m_Input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); _Mouse_Position.text = "x:" + Input.GetAxis("Horizontal").ToString() + " " + "y:" + Input.GetAxis("Vertical").ToString(); switch (kcode) { //Keyboard-WASD case KeyCode.W: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.A: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.S: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.D: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; //Keypad-Arrow Keys case KeyCode.DownArrow: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.UpArrow: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.LeftArrow: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; case KeyCode.RightArrow: rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * Speed, 0)); rb.AddForce(new Vector2(0, Input.GetAxis("Vertical") * Speed)); break; ////Fire Stick Only //case KeyCode.Joystick1Button0: // break; //case KeyCode.LeftArrow: // break; //case KeyCode.DownArrow: // if (transform.position.y > MinHeight) { targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement); } // break; //case KeyCode.RightArrow: // break; //case KeyCode.UpArrow: // if (transform.position.y < MaxHeight) { targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement); } // break; //case KeyCode.Escape: // break; ////Fire Stick Only //case KeyCode.Menu: // break; } } } } } } catch { Debug.Log("Error"); } } private float Positon_X(float Postion) { float x = 0f; if (Postion < 900 && Postion > 600) { return Postion; } if (Postion > 900){ return 900f; } else if (Postion < 600f) { return 600f; } return x; } private float Positon_Y(float Postion) { float y = 0f; if(Postion > 160 && Postion < 700) { return Postion; } if(Postion > 700) { return 700f; } else if (Postion < 160f) { return 160f; } return y; } public void Droid_Up_Click() { if (Health > 0) { if (transform.position.y < MaxHeight) { targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement); } } } public void Droid_Down_Click() { if (Health > 0) { if (transform.position.y > MinHeight) { targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement); } } } public void Hurt_Player() { Player_Anim.Play("Player_Dizzy"); } public void Hurt_Player_SoundFX() { FindObjectOfType<AudioManager>().Play("dizzy"); } } currently when I press the WASD keys I can see in the position text it wants to move but the player is not moving
thats a lot of code to go through- it might make debugging hard. Any way you can duplicate the object you want to move, pull it out of the hierarchy, and start messing with the standalone object? I'm guessing the problem lies somewhere in the update method. But also, if a parent is linked to a child, it can constrain certain movements like translation or rotation. Bottom Line, I would simplify the hierarchy and code for testing, to isolate the issue. But seeing the Position wanting to change is a good start. Best of luck! -Z
There are several confounding problems with how you're handling keyboard events. First, you're iterating over every possible key code, just to check if a couple of them have been pressed this frame. This is totally redundant; there's no functional difference between doing the foreach/getkey/switch and just having a chain of Input.GetKeyDown calls: Code (CSharp): if( Input.GetKeyDown( KeyCode.W ) || Input.GetKeyDown( KeyCode.LeftArrow ) ) { // do up stuff } else if( Input.GetKeyDown( KeyCode.A ) || Input.GetKeyDown( KeyCode.LeftArrow ) ) { // do left stuff } else if( Input.GetKeyDown( KeyCode.S ) || Input.GetKeyDown( KeyCode.RightArrow ) ) { // do right stuff } else if( Input.GetKeyDown( KeyCode.D ) || Input.GetKeyDown( KeyCode.DownArrow ) ) { // do down stuff } Second, inside every iteration, you're also setting m_Input to GetAxis( "Horizontal" ) and GetAxis( "Vertical" ). This makes the entire foreach/getkey/switch construct doubly redundant, because GetAxis( "Horizontal" ) and GetAxis( "Vertical" ) by default responds to both WASD and arrow key presses. Third, inside every switch statement, you're again querying against GetAxis to add force to the rigidbody, making it triply redundant. Fourth, none of this actually matters, because you're totally ignoring the rigidbody physics and seemed to have left a Vector2.MoveTowards in your keyboard control code on line 116 which doesn't respond to keyboard input at all and instead responds to the mouse. To clean this up, take out all of the redundant code. Decide whether you're moving by physics or by transform position. If you're moving by transform position, your entire keyboard movement section can be replaced by two lines: Code (CSharp): var vh = new Vector3( Input.GetAxisRaw( "Horizontal" ), 0, Input.GetAxisRaw( "Vertical" ) ); transform.position += vh.normalized * Speed * Time.deltaTime;