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

2D non-Rigidbody Frogger rotate

Discussion in 'Scripting' started by ZeroXGames, Jul 20, 2016.

  1. ZeroXGames

    ZeroXGames

    Joined:
    Jul 20, 2016
    Posts:
    4
    Hello new to this forum. I'm currently working on a frogger clone just for practice and for the fun.
    I'm currently stuck on rotating my player to face the direction based on input.

    Attached a .txt of my player script. I was going to try and control rotation based on bools, however I still run into the issue of constant rotation. example press W object faces up D object face right.

    Any help will be greatly appreciated thank you in advance.
     

    Attached Files:

  2. topsekret

    topsekret

    Joined:
    Apr 18, 2013
    Posts:
    69
    Haven't run your code, but just from looking at it, here are a few thoughts:

    1. Your state variables _up and _down are never set, so they will always have the default value of false, meaning the your RotateFrog function will never do anything.

    2. Suppose that _up and _down were set appropriately based on the direction the frog is facing. Even then, because you are using Transform.Rotate, it is going to rotate by that amount relative to the current rotation. So _playerTransform.Rotate(0, 0, 180) means rotate around the z axis 180 from where I currently am. So if _up were true and _down were false, if you call the RotateFrog function on the first frame, the frog will be looking down, but if you call it on the next frame, it will now be looking up again because 180 + 180 = 360 => you rotated back to the starting orientation.

    It seems like you are expecting this function to set the rotation based on whether which flags are set. So if _up is true, all other flags should be false and the transform will always be rotated so that it is looking up. If that is what you want, use _playerTransform.rotation = Quaternion.euler(0, 0, 180) (of course, swap out 180 for whatever angle you need).

    Basically, the rotation member is analogous to the position member, as the Rotate function is analogous to the Translate function.
     
  3. ZeroXGames

    ZeroXGames

    Joined:
    Jul 20, 2016
    Posts:
    4
    Thank you for letting me know about Quaternion.euler. seems to work perfect. one bug I am encountering is when W is pressed frog moves up press a second time frog continually moves downwards. I packaged the project file for any one who would like to see and debug.

    updated script attached
     

    Attached Files:

  4. ZeroXGames

    ZeroXGames

    Joined:
    Jul 20, 2016
    Posts:
    4
    Just wanted to add an update. I got it working thank you again for the help. working on animation right now.
     
  5. topsekret

    topsekret

    Joined:
    Apr 18, 2013
    Posts:
    69
    Lol, I was just about to post the solution. Reading the _down variable an extra time instead of _left, right?
     
  6. ZeroXGames

    ZeroXGames

    Joined:
    Jul 20, 2016
    Posts:
    4
    Yep lol that was it.
     
    topsekret likes this.