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

Cannot move cube across the screen

Discussion in 'Editor & General Support' started by magicscreen, Jul 20, 2015.

  1. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    I am currently taking the C# course and I created a script that looks exactly like the script the instructor wrote.

    This is the part of the script that causes the cube to move across the screen based on the mouse:

    float xMovement = Input.GetAxis ("Mouse X");
    if (xMovement > 0) {
    transform.eulerAngles += Vector3.up * xMovement * Time.deltaTime * horizontalVelocity;
    }

    The only Rotational axis that changes is the Y axis but the mouse stays in one place, it just pivots.

    I tried changing the Vector3.up to transform.up but it made no difference.

    The cube just pivots around in a circle around the Y axis. The X and Z rotational values remain at 0 no matter how I move the mouse.

    I can also move the cube using keycodes and that works as expected.

    This is the entire script. The velocity and horizontalVelocity are entered in the Inspector:

    ---------------------------------------------------------------------------------------------------------------------------------------

    public class PlayerManager : MonoBehaviour {
    public float velocity, horizontalVelocity;

    void Update () {
    float xMovement = Input.GetAxis ("Mouse X");

    if (xMovement > 0) {
    transform.eulerAngles += Vector3.up * xMovement * Time.deltaTime * horizontalVelocity;
    }

    if (Input.GetKey(KeyCode.W)) { //Forward
    transform.position += transform.forward * velocity * Time.deltaTime; //current facing forward
    }
    if (Input.GetKey(KeyCode.S)) { //Bac
    transform.position -= transform.forward * velocity * Time.deltaTime; //current facing backward
    }
    if (Input.GetKey(KeyCode.A)) { //Left
    transform.position -= transform.right * velocity * Time.deltaTime; //current facing right
    }
    if (Input.GetKey(KeyCode.D)) { //Right
    transform.position += transform.right * velocity * Time.deltaTime; //current facing fleft
    }
    }

    ---------------------------------------------------------------------------------------------------------------------------------
    I attached a Word document with some pictures of the Inspector before and after I click the Run button.

    Thanks for any help given.
     

    Attached Files:

  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    transform.eulerAngles is the rotation of your object in a Vector3 format, and Vector3.Up is the direction of world-space up. Vector's can be used for many things, and they can be confusing at times, but it will benefit you greatly to learn how to tell when a Vector is being used as a position, direction, rotation, etc. and know not to mix-and-match them unless you know what the result will be.


    This "if" block will only run if you move your mouse to the right.


    Again, transform.eulerAngles is a rotation: Vector3( XRotation, YRotation, ZRotation).
    Vector.up is a direction: Vector3(0, 1, 0)

    So when you add Vector.up to transform.eulerAngles, you'll see that you are only changing the YRotation part of your rotation vector.



    instead, I recommend:
    Code (CSharp):
    1. transform.eulerAngles += new Vector3(xMovement * horizontalVelocity, yMovement * verticalVelocity, 0)* Time.deltaTime;
    You will have to get xMovement AND yMovement from the mouse, and you'll have to change/remove your if statement so that line runs whenever xMovement or YMovement isn't 0



    Hope this helps!
     
  3. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42

    Gambit MSplitz, thanks for your feedback. I am a novice in gaming. I need to better understand what the various built in functions do and how they work..

    The script that I posted had an error as you caught it.

    The if statement should have been:

    if (xMovement != 0) and NOT if (xMovement > 0)

    You are also correct when you said that only the Y Rotation changes.

    I also tried to change Vector3.up to transform.up and that too did not work. Now based on your explanation I understand why.

    I believe that is the reason for my issue. I need to move the cube around and not just pivot around the Y axis.

    Also, I am in the process of taking the C# course and basically copied the instructors script because I could not get mine to work.

    His works in his video but mine does not.

    I tried your suggestions and still cannot get the cube to move around the screen.

    This is what I wrote:
    ---------------------------------------------------------------------------------------------------------------------------------------------
    Code (csharp):
    1.  
    2.  
    3. public float velocity, horizontalVelocity, verticalVelocity;
    4.  
    5.  void Update ()  {
    6.  
    7.  float xMovement = Input.GetAxis ("Mouse X");
    8.  float yMovement = Input.GetAxis ("Mouse Y");
    9.  
    10.  transform.eulerAngles += new Vector3 (xMovement * horizontalVelocity, yMovement * verticalVelocity, 0) * Time.deltaTime;
    11. }
    12.  
    13.  
    I set the Horizontal and Vertical velocities in the Inspector.
    -------------------------------------------------------------------------------------------------------------------------------------------------

    What I am trying to do is use the mouse to control the movement of the cube in all directions.

    I have a feeling that I may have a setting wrong in the Inspector or somewhere else.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Because you are changing your transform.eulerAngles, all that will change if your rotation.

    What you want to do is change your transform.position, like so:
    Code (CSharp):
    1. transform.position += new Vector3 (xMovement * horizontalVelocity, yMovement * verticalVelocity, 0) * Time.deltaTime;
     
  5. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    Thanks for your reply.

    I will try it.
     
  6. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    Gambit MSplitz,

    Thanks for your earlier reply.

    I have a script that has two cubes with one cube (Enemy) supposed to be following another cube (Player) around the screen.

    You explained that the transform.eulerAngles is for rotation and gave me other suggestions. Your suggestions worked for moving the Player around the screen but was not able to rotate it.
    .
    In the script below the statement that sets the transform.eulerAngles also uses the Vector3.up and xMovement to move and rotate the Player cube around the screen with the Enemy cube following it.

    I just got finished going over my script with the teacher of the Unity C# class. He copied it and ran it on his computer which I saw using Skype. On my computer, the Player cube only rotates in one spot. He could not figure it out and said it might be an issue with Unity. I don't think it is a Unity issue but rather some setting.

    This is my script:
    ---------------------------------------------------------------------------------------------------------
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.  //public float velocity, horizontalVelocity, verticalVelocity;
    8.  public float velocity, horizontalVelocity;
    9.  
    10.  void Update ()  {
    11.  
    12.  // Update is called once per frame
    13.  // Check key presses here since they will be checked for a press in each frame
    14.  // GetKey will keep running code while key is pressed
    15.  // GetKeyDown will only run code once when the key is pressed even if held down
    16.  // GetKeyUp will run code only once when the key is released
    17.  // deltaTime is in Class Time and controls the frame time
    18.  
    19.  float xMovement = Input.GetAxis ("Mouse X");
    20.  //float yMovement = Input.GetAxis ("Mouse Y");
    21.  
    22.  if (xMovement != 0) {
    23.  transform.eulerAngles += Vector3.up * xMovement * Time.deltaTime * horizontalVelocity;
    24.  print ("Rotated " + xMovement);
    25.  }
    26.  
    27.  //transform.position += new Vector3 (xMovement * horizontalVelocity, 0, yMovement * verticalVelocity) * Time.deltaTime;
    28.  
    29.  //Forward
    30.  if (Input.GetKey(KeyCode.W)) {
    31.  //print ("keyCode: W");
    32.  transform.position += transform.forward * velocity * Time.deltaTime; //current facing forward
    33.  
    34.  }
    35.  //Backward
    36.  if (Input.GetKey(KeyCode.S)) {
    37.  transform.position -= transform.forward * velocity * Time.deltaTime; //current facing backward
    38.  }
    39.  //Left
    40.  if (Input.GetKey(KeyCode.A)) {
    41.  transform.position -= transform.right * velocity * Time.deltaTime; //current facing right
    42.  }
    43.  //Right
    44.  if (Input.GetKey(KeyCode.D)) {
    45.  transform.position += transform.right * velocity * Time.deltaTime; //current facing left
    46.  }
    47.  
    48.  }
    49.  
    50. }
    51.  
    The teacher copied it and ran it on his computer and it worked as expected. He was able to rotate and move the Player cube around the screen with the Enemy cube following it.
    On my computer the Player cube only rotates in one spot.

    Any help would be welcome.
    --------------------------------------------------------------------------------------------------------------------------
     
  7. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    You put your rotation code inside this if statement, which means the cube will only rotate when you move your mouse left and right. As long as that's what you meant to do, everything looks fine and I don't see a problem.

    What do you mean by it only rotates in one spot? Does it only rotate on one axis, or maybe only when it's in a certain position?
     
  8. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    What I meant is that the cube seems to rotate around the Y axis in place.
    The rotation code is inside the if statement.

    If the cube didn't move for everyone I would be less upset.
    The fact that the cube rotates and moves for the teacher and not me is confusing.
    He downloaded and used my script successfully but I cannot get it to work.

    The only thing I can think of is that there is some setting, maybe the Horizontal and/or Vertical setting under Project Settings that is set incorrectly or some other Unity setting.

    Thanks for your response.
     
  9. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    The cube doesn't move even when you press W, S, A, or D?
     
  10. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    The WSDA keys will move the cube in the specified direction, this works.

    The mouse does not work.

    I tried playing with the Project Settings Input items but nothing seemed to work.

    Thanks again for your help. I really appreciate it.
     
  11. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It looks to me like it should work if you uncomment these lines:


    And comment this line:

     
  12. magicscreen

    magicscreen

    Joined:
    May 19, 2015
    Posts:
    42
    Thanks once more for your feedback.

    It kind of works with the changes you suggested.

    The changes you suggested I already tried and it does allow me to move the cube around the screen.

    However, I cannot move the cursor back and forth over the cube and have it rotate.

    I was trying to complete a class project and the script without making the changes is supposed to have the cube move around the screen and, if you move the cursor back and forth over the cube, rotate the cube.

    The teacher got onto my computer to look at the script and how it functioned and he could not figure out what was happening .

    He even took an exact copy of my script, put it on his computer and it worked the way it was supposed to work.

    This tells me that the issue is computer specific and the only thing I can think of is that there is a setting somewhere in Unity that is causing the issue. I am not going to worry about it anymore. If I need to also rotate the cube or other object I will write some additional code.

    Thanks again for your help.

    Art