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

Two guys standing on sphere... both standing upright, and looking at each other?

Discussion in 'Scripting' started by Deleted User, Dec 19, 2015.

  1. Deleted User

    Deleted User

    Guest

    Ok so I know how to get them both to stand upright. The center of the sphere is Vector3(0,0,0)... so to stand upright, I do this to both guys:

    transform.LookAt(Vector3.zero, Vector3.up);

    This makes them stand up, but they are looking toward the north pole.

    Now, to make them look at each other, I could do this:

    transform.LookAt(OTHERGUY, Vector3.up);

    But now they flop around on the ground like two headless chickens. They are looking at each other, but they aren't standing upright anymore.

    How do I get them to look at each other, and stand upright?

    I imagine this question's been asked a million times. Honestly, I've had to deal with this same issue myself before, but I lost the code. Shouldn't there just be a built in function to set both the forward direction *and* the up direction of a vector3? Would save this question from having to be asked anymore. But I seem to remember I had to use multiple lines to solve this problem before.
     
  2. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    I am not very sure.. but it sounds that you need to do a height correction... set a new vector3 and make the player look at that vector.. the new vector will get the values from the otherguy like this, leaving the y value untouched so that the guys keep looking straight.

    vector3 newPos = new Vector3( otherGuy.position.x, transform.position.y, otherGuys.position.z);
     
  3. Deleted User

    Deleted User

    Guest

    Figured it out...

    .Lookat() is all that's needed to do this. Just have to feed it the right vector3's...

    My first problem was my models weren't looking toward their own Z arrow, nor was their heads pointing toward their own Y arrows. So I had to do this:

    1) adjust my "guys" gameobject so that their heads point toward the y arrow, and they are facing toward their own Z arrow. Had to put them inside an empty parent gameobject, so I could freely rotate them to get them to line up with the parent's Y and Z arrows to accomplish this.

    2) Then I simply use this code on their parent gameobjects: guy1.transform.LookAt(guy2.transform.position, guy1.transform.position);

    This sets my "forward" to look at guy2, and my "up" direction is... me. That sets my "up" angle from the center of the planet (which is 0,0,0), pointing towards me (guy1). Same as if you drew an arrow from the center of the planet, to guy1. That's the "up" direction guy1's head should point.

    Of course, if the planet isn't at 0,0,0, then this wouldn't work. You'd then need to do something like:

    guy1.transform.LookAt(guy2.transform.position, guy1.transform.position - planet.transorm.position);

    Hope this helps someone.