Quantcast
Channel: Answers by "ThePersister"
Viewing all articles
Browse latest Browse all 99

Answer by ThePersister

$
0
0
Hey @TheGameCreator18, If you look at the code you posted here, **at line 49, your "Reverse Control" if statement ends early!** That means that before reaching Y 199, you'd always Move Left (Normal + Reverse) and Right (Normal + Reverse) at the same time when pressing A or D, Normal + Reverse together meaning **No Movement Sideways**. To fix this, **move the } to line 59**. I noticed another danger and that is: **PlayerPosY < 199 combined with PlayerPosY >= 198, at player Y == 198, the player will move both normal and reverse, so it won't move.** To fix that, make it an **if else statement instead of a double if.** Noticed that you use **"transform.right"** instead of **"playerTransform.right"** for moving sideways, which will not make it invert correctly. Noticed that you're **NOT applying PlayerPosX in your sideways movement**, even though you set it. Noticed (During refactoring, fixed that for you in the finished code): // When reaching 199, jumps to 400, warning this will lock the player on Y 400. PlayerPosY = reverseControl ? 400 : playerTransform.position.y; **Conceptually, I think you have a 2D Vertical Scroll Shooter game, where you move from 0 to 200 and then reverse from 400 to 200, is that correct?** On top of that I cleaned up your code a bit. You can adjust it to your needs, but this should look / feel cleaner. using UnityEngine; using System.Collections; public class PlayerMovementFix : MonoBehaviour { [Header("Player Reference")] public GameObject playerInstance; private Transform playerTransform; [Header("Player Movement")] public float PlayerSpeed_Current = 5f; public float PlayerSpeed_Min = 2f; public float PlayerSpeed_Max = 10f; public float PlayerSpeed_Sideways = 0.5f; public float PlayerPosX, PlayerPosY, PlayerPosZ, PlayerRotX; [Header("Other Stats")] public bool BossArrived; void Update () { #region PlayerMovement playerTransform = playerInstance.transform; // Noticed original danger: PlayerPosY < 199 && PlayerPosY >= 198, at player Y == 198, the player will move both normal and inverse, so it won't move. bool reverseControl = PlayerPosY >= 198; int sideways = 0; // Move up, down, left and right. And reversed if reverseControl == true. if (Input.GetKey(KeyCode.S)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current + 1 : PlayerSpeed_Current - 1; if (Input.GetKey(KeyCode.W)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current - 1 : PlayerSpeed_Current + 1; if (Input.GetKey(KeyCode.A)) sideways = reverseControl ? 1 : -1; if (Input.GetKey(KeyCode.D)) sideways = reverseControl ? -1 : 1; if (reverseControl) { // Rotate player 180. (Only do this once) if (PlayerRotX == 0) { PlayerPosY = 400; // Set Y to 400 once. playerTransform.Rotate(180, 0, 0, Space.World); PlayerRotX = 1; } } // Makes sure PlayerSpeed is always at least 2 and at most 10. PlayerSpeed_Current = Mathf.Clamp(PlayerSpeed_Current, PlayerSpeed_Min, PlayerSpeed_Max); // Move up or down, but only if there's no boss arrived. if (!BossArrived) { Vector3 upOrDown = (reverseControl ? Vector3.down : Vector3.up); playerTransform.Translate(upOrDown * Time.deltaTime * PlayerSpeed_Current); } // Sideways movement. if (sideways != 0) { bool left = (sideways == -1); if (left) { playerTransform.position -= playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime; } else { playerTransform.position += playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime; } } #endregion // Check Player Visible, Pause and Exit. _checkPlayerVisible(); _checkTogglePause(); _checkEscape(); } private void _checkPlayerVisible() { // Reset X to 10 if Player is no longer visible. if (!playerInstance.GetComponent().isVisible) { PlayerPosX = 10; playerTransform.position = new Vector3(PlayerPosX, playerTransform.position.y, PlayerPosZ); } } private void _checkTogglePause() { if (Input.GetKeyDown(KeyCode.P)) { Time.timeScale = (Time.timeScale == 1) ? 0 : 1; } } private void _checkEscape() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } } } I hope that helps, if it did, please accept this answer, it'd be much appreciated! If you have any other questions, let me know! :) Cheers, ThePersister

Viewing all articles
Browse latest Browse all 99

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>