Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change orientation according to movements

Discussion in 'Scripting' started by ThisIsSparta, Jul 26, 2016.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hi guys! I have a problem. I'm working on a grid movement system and everything works fine except for the fact I want to change when the player moves the orientation towards the node he is moving! I'll attach the unity package! if you can take a look and help me I'll be more then happy! thank you very much!
     

    Attached Files:

  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Edit your move Coroutine to this.

    Code (CSharp):
    1.     IEnumerator PerformMove() {
    2.         isMoving = true;
    3.         float elapsedTime = 0f;
    4.         Vector3 initialPosition = transform.position;
    5.         Vector3 inititalUp = transform.up;
    6.  
    7.         this.transform.rotation = Quaternion.LookRotation (this.positionToMoveTo - this.transform.position, Vector3.up);
    8.  
    9.         while (elapsedTime < timeToMove) {
    10.             transform.position = Vector3.Lerp(initialPosition, positionToMoveTo, (elapsedTime / timeToMove));
    11.             // transform.up = Vector3.Lerp(inititalUp, transformUp, (elapsedTime / timeToMove));
    12.             elapsedTime += Time.deltaTime;
    13.             yield return null;
    14.         }
    15.  
    16.         transform.position = positionToMoveTo;
    17.         // transform.up = transformUp;
    18.  
    19.         positionToMoveTo = invalidPosition;
    20.         isMoving = false;
    21.     }
     
  3. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    hey dude thx! I think you are on the right path but something still missing. I tested it with a cube as a player and the orientation of the commands change but the cube is orientated ever in the same direction :(

    the cube is not rotating the direction he's facing! that's the problem :(
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Try to add something to allow you to see if it is changing orientation. If something is symmetrical you can't tell if its rotating because it snaps. Unless the transform component says 0,0,0 in the inspector then we have a problem.
     
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I added some 'eyes' to allow me see where the capsule was facing. Seems to work for me!
     

    Attached Files:

  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Also, one more thing. It seems the navigation is based on the local rotation not global rotation. So if the concept of LEFT, RIGHT, FORWARD, BACKWARD is based on the current orientation. So if I were to press RIGHT or the D key the player effectively moves in a circle, if this is intentional awesome! if not you have a logical error!
     
  7. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hey man! I guess I was sleepy! It works! the only problem is when the player change direction he face from the start of the movement the direction where is going and not lerpinng between the actual orientation and the orientation he have to go. is this solvable in your opinion? thank you very much!
     
  8. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I'm not sure I understand the issue for the orientation.