Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Helo With pacman game movement

Discussion in 'Scripting' started by gdossantos87, May 8, 2015.

  1. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    hey guys, im a new developer and im trying to build a pacman game just for fun and practice, im scripting the movement of the player witch is basically one constant movement and just changing the rotation

    the thing is that sometimes i get a delay from the rotation and im not sure if its the script or some other component in the game object plz help me find something wrong with the script



    hey developersss que xopas!, estoy creando un juego estilo pacman solo para practicar y quiero optimizar los movimiento de mi player

    la cosa es que siento que aveces el tiempo de respuesta no es el mas optimo y no se si es algun problema de codigo

    es un script sencillo en donde mantengo un movimiento continuo hacia adelante y solo cambio la rotacion, la cosa funciona pero aveces demora 1 milesima de segundo y por supuesto se siente
    Code (csharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour
    3. {
    4. public float moveSpeed = 7f;
    5. public bool moveUp = false;
    6. public bool moveDown = false;
    7. public bool moveLeft = false;
    8. public bool moveRight = false;
    9.  
    10.  
    11. private Rigidbody playerRb;
    12. private Transform playerRotation;
    13. private PlayerDeath playerDeath;
    14.  
    15.  
    16. // Use this for initialization
    17. void Awake ()
    18. {
    19. playerRotation = GetComponent<Transform>();
    20. playerRb = GetComponent<Rigidbody>();
    21. playerDeath = GetComponent<PlayerDeath>();
    22.  
    23. }
    24.  
    25. // Update is called once per frame
    26. void FixedUpdate () {
    27. MoveRotation();
    28. ContinuousMovement();
    29.  
    30. }
    31.  
    32. public void ContinuousMovement()
    33. {
    34. if (playerDeath.isDead != true)
    35. {
    36. playerRb.MovePosition(transform.position + transform.forward * moveSpeed * Time.deltaTime);
    37. }
    38. else
    39. {
    40. moveSpeed = 0f;
    41. }
    42. }
    43.  
    44. void MoveRotation()
    45. {
    46. if (Input.GetKeyDown(KeyCode.DownArrow) && moveDown != true)
    47. {
    48. moveDown = true;
    49.  
    50. playerRotation.rotation = Quaternion.Euler(0f, 180f, 0f);
    51. moveUp = false;
    52. moveLeft = false;
    53. moveRight = false;
    54. }
    55.  
    56. if (Input.GetKeyDown(KeyCode.UpArrow) && moveUp != true)
    57. {
    58. moveUp = true;
    59. playerRotation.rotation = Quaternion.Euler(0f, 0f, 0f);
    60. moveDown = false;
    61. moveLeft = false;
    62. moveRight = false;
    63. }
    64.  
    65. if (Input.GetKeyDown(KeyCode.LeftArrow) && moveLeft != true)
    66. {
    67. moveLeft = true;
    68.  
    69. playerRotation.rotation = Quaternion.Euler(0f, -90f, 0f);
    70. moveUp = false;
    71. moveDown = false;
    72. moveRight = false;
    73. }
    74.  
    75. if (!Input.GetKeyDown(KeyCode.RightArrow) && moveRight != true) return;
    76. moveRight = true;
    77.  
    78. playerRotation.rotation = Quaternion.Euler(0f, 90f, 0f);
    79. moveUp = false;
    80. moveLeft = false;
    81. moveDown = false;
    82. }
    83.  
    84.  
    85.  
    86.  
    87.  
    88. }
    89.  
    90.  
     
    Last edited: May 8, 2015
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    When I tried the script on a box, sometimes the object would not respond when I pressed an arrow key. Is this what you mean by delay? I was able to fix it by changing Fixed update to Update.
     
  3. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    Thats correct Thx u!!!! i left a fixedUpdate() for the object to move because it has physics, and for the imput a Update()
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    key presses need to be handled inside of Update()

    FixedUpdate() and Update() happen at different times at different rates, if you want to find out more about that check here: http://docs.unity3d.com/Manual/ExecutionOrder.html


    also,
    [ code] [ /code] tags when you paste in code, it formats it correctly :)
     
  5. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    thx u verry much Lefty
     
  6. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    This is my fixed code

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement1 : MonoBehaviour {
    6.  
    7.     public float moveSpeed = 7f;
    8.  
    9.  
    10.     private Rigidbody playerRb;
    11.     private Transform playerRotation;
    12.    
    13.  
    14.     // Use this for initialization
    15.     void Awake()
    16.     {
    17.         playerRotation = GetComponent<Transform>();
    18.         playerRb = GetComponent<Rigidbody>();
    19.     }
    20.     // Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.        
    24.         ContinuousMovement();
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.          MoveRotation();
    30.     }
    31.  
    32.     public void ContinuousMovement()
    33.     {
    34.        
    35.             playerRb.MovePosition(transform.position + transform.forward * moveSpeed * Time.deltaTime);
    36.        
    37.        
    38.     }
    39.     void MoveRotation()
    40.     {
    41.         if (Input.GetKeyDown(KeyCode.DownArrow))
    42.         {          
    43.             playerRotation.rotation = Quaternion.Euler(0f, 180f, 0f);            
    44.         }
    45.         if (Input.GetKeyDown(KeyCode.UpArrow))
    46.         {  
    47.             playerRotation.rotation = Quaternion.Euler(0f, 0f, 0f);
    48.         }
    49.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    50.         {      
    51.             playerRotation.rotation = Quaternion.Euler(0f, -90f, 0f);                      
    52.         }
    53.         if (Input.GetKeyDown(KeyCode.RightArrow))
    54.         {
    55.             playerRotation.rotation = Quaternion.Euler(0f, 90f, 0f);
    56.         }
    57.     }
    58. }
    59.  
    60.  
     
  7. TStudios13

    TStudios13

    Joined:
    Jun 10, 2019
    Posts:
    3
    Do you mind if I use this script in my project??