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

(Solved) Rotation and Movement - Need help with script!

Discussion in 'Scripting' started by juliushg, Apr 22, 2015.

  1. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    I hope you can help me if I can explain this in a comprehensive way.

    I'm making a monster to translate by itself in a maze (this is turn-based, so the monster makes the movement in any time needed). I made this by using Unity units as cells, then I made the monster to take its own decision and then move according to its choice. The plane uses X and Z to the four directions. The monster is for now a simple ball with two spheres like "eyes" to see its orientation.

    To decide where to move, I used boolean variables like this:

    Code (CSharp):
    1. dirN = !Physics.raycast(transform.position, Vector3.forward, 1);
    2. dirE = !Physics.raycast(transform.position, Vector3.right, 1);
    3. dirS = !Physics.raycast(transform.position, Vector3.back, 1);
    4. dirW = !Physics.raycast(transform.position, Vector3.left, 1);
    That is, for example, if dirN returns true, the monster can move to north, and so on, if false, it can't. Like this: if dirN then can move north, but if !dirN can't.

    At that point everything is behaving correctly, the monster is moving well along the maze and selecting only the "empty" cells to move there. But then I decided to rotate the monster (along its Y axis) when he takes the choice. I made it to rotate (an "instant" rotation, no need to rotate smoothly) to the new direction with:

    Code (CSharp):
    1. Vector3 rot = transform.rotation.eulerAngles; transform.rotation = Quaternion.Euler(rot.x, 180,rot.z); // to dirN
    2. Vector3 rot = transform.rotation.eulerAngles; transform.rotation = Quaternion.Euler(rot.x, 270,rot.z); // to dirE
    3. Vector3 rot = transform.rotation.eulerAngles; transform.rotation = Quaternion.Euler(rot.x, 0,rot.z); // to dirS
    4. Vector3 rot = transform.rotation.eulerAngles; transform.rotation = Quaternion.Euler(rot.x, 90,rot.z); // to dirW
    Now, it's strange. The monster no longer "respects" the walls. He enters the walls (as if the raycast is not pointing to the correct direction). I put debugging lines to see what's happening, but it's reporting correctly.

    Resuming:

    WORKING

    -The monster enters "moving mode".

    -Selects a direction in a function: "int DecideDirection()" based in raycasting the four directions (that routine is correct whenever the raycast is doing well).

    -The function returns a direction to move: monsterDir, and starts the movement turning on a flag: isMMoving

    -If the flag is on, then in the void update an if statement moves the monster to its new position:

    Code (CSharp):
    1.     if (isMMoving){
    2.         transform.position = Vector3.MoveTowards (transform.position, targetMPosition,  speed*Time.deltaTime);
    3.  
    4.         if (transform.position == targetMPosition) {isMMoving = false;}    // when target position is reached, stops the movement
    5.        
    6.     }
    -Stops movement and waits to another movement.

    This method works for me, the monster moves smoothly until reaches the new position in the adjacent cell. Until here, the monster is moving without any rotation, only fixed in its axis.

    Now the following is...

    NOT WORKING

    -The monster enters "moving mode".

    -Selects a direction in a function: "int DecideDirection()" based in raycasting the four directions (that routine is correct whenever the raycast is doing well).

    -The function returns a direction to move: monsterDir, and starts the movement turning on a flag: isMMoving

    -Four if statements decide where to rotate the monster (I see the orientation of the monster with the "eyes" I put on the ball):

    Code (CSharp):
    1. if (monsterDir ==1 && !monsterRotated) {
    2.                 Vector3 rot = transform.rotation.eulerAngles;
    3.                 transform.rotation = Quaternion.Euler(rot.x, 180,rot.z);
    4.                 monsterRotated=true;
    5.             }
    6.  
    7.             if (monsterDir ==2 && !monsterRotated) {
    8.                 Vector3 rot = transform.rotation.eulerAngles;
    9.                 transform.rotation = Quaternion.Euler(rot.x, 270,rot.z);
    10.                 monsterRotated =true;
    11.             }
    12.  
    13.             if (monsterDir ==3 && !monsterRotated) {
    14.                 Vector3 rot = transform.rotation.eulerAngles;
    15.                 transform.rotation = Quaternion.Euler(rot.x, 0, rot.z);
    16.                 monsterRotated =true;
    17.             }
    18.  
    19.             if (monsterDir ==4 && !monsterRotated) {
    20.                 Vector3 rot = transform.rotation.eulerAngles;
    21.                 transform.rotation = Quaternion.Euler(rot.x, 90, rot.z);
    22.                 monsterRotated =true;
    23.             }
    Note: monsterDir values 1, 2, 3, 4 are N, E, S, W.

    -If the flag is on, then in the void update an if statement moves the monster to its new position:

    Code (CSharp):
    1.     if (isMMoving){
    2.         transform.position = Vector3.MoveTowards (transform.position, targetMPosition,  speed*Time.deltaTime);
    3.  
    4.         if (transform.position == targetMPosition) {isMMoving = false;}    // when target position is reached, stops the movement
    5.        
    6.     }
    -Stops movement and waits to another movement.

    In this last setup, the monster enters throughthe walls, but its rotation is displaying correctly. The inspector reports me the correct Rotation values. But I understand that something is wrong with the orientation of the game object and its relative translation values.

    Please help me! and tell me if you need more information or if you think I'm doing something wrong or need another method (something like LookAt or anything).
     
  2. juliushg

    juliushg

    Joined:
    Mar 27, 2015
    Posts:
    18
    Sorry, I finally solved this problem after a few hours of reasoning and researching. Now, I'm new in this forum, I don't know if I have to delete this thread or explain the solution here. Please tell me.