How to rotate an object so it's Y axis points away from the world origin

Hello!

I ask something: how would one go about generating a quaternion that makes the top of the object point away from the world origin?

The quaternion will be used to rotate an object while instantiating it, as my game uses radial gravity (i.e: player and enemies walk around the surface of the planet) and needs the enemies and player to point towards the world origin when spawning (This is where the center of the planet is) so that the raycast used by my physics script picks up the planetâ€™s surface, and thus applies the proper gravity. At the moment though, my characters instantiate with what looks like the default rotation!

Thanks for all the help given!

Assuming your modelsâ€™ local â€˜Yâ€™ vector is their up vector, couldnâ€™t you just determine their orientation based on the difference of where theyâ€™re instantiated in world coordinates and the target origin, and set this new vector as their up vector?

Yes, my model (or, my parent object actually! ) uses local Y direction for everything, as public Y obviously doesnâ€™t point outwards from the planet.

And wouldnâ€™t transformPoint (or the reverse) help with this? or not?

And the way you have explained this tells me youâ€™re saying that the objectâ€™s rotation can be generated by using Quaternion.LookRotation and the difference between (0,0,0) and . I have already tried this, and for some odd reason, it didnâ€™t workâ€¦

Okâ€¦ I tried LookRotation again, itâ€™s sort of working, except the X axis, not the Y axis, is pointing at the origin!

The heckâ€™s going on?! O.O

Ok, fixed it!

Set the up direction to, weirdly, the sideâ€¦ and it works!

Will inform if something else goes horribly wrongâ€¦ Also, I instantiate with default rot, and then I set it to the rotation I want after.

I take back what I saidâ€¦
it still does it, and itâ€™s very inconsistent: it will do it correctly most of the time, but then at others, the vector is COMPLETELY WRONG!

``````using UnityEngine;
using System.Collections;

public class TestingUpVector : MonoBehaviour
{

public Transform characterTransform;
public Transform originTransform;
public float lookAroundDirection = 0f; // turn the character based on this value
public float rayDistance = 1.5f;

void OnDrawGizmos()
{
if (characterTransform != null && originTransform != null)
{
if (enabled)
{
Gizmos.color = Color.black;
Gizmos.DrawLine(originTransform.position, characterTransform.position);
Gizmos.color = Color.green;
Gizmos.DrawRay(characterTransform.position, (characterTransform.right * rayDistance));
Gizmos.color = Color.yellow;
Gizmos.DrawRay(characterTransform.position, (characterTransform.up * rayDistance));
}
}
else Debug.Log("Either one or both Transforms are null for Monobehavior: TestingUpVector");
}

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
Logic();
}

void Logic()
{
// These 3 lines describe how I'm thinking of your problem and how it can be solved
Vector3 resultUp = characterTransform.position - originTransform.position;
characterTransform.up = resultUp; // assign the up vector
Quaternion currentOrientation = characterTransform.localRotation; // find out what the character's  new local rotation is now after the assignment

// this line is extra to show you can also change the direction your character is looking after instantiating
Quaternion turnRotation = Quaternion.Euler(0, lookAroundDirection, 0); // assuming we just want to look around us

// Apply the rotations to the localRotation. You don't need turnRotation, it's just here to show you additional options.
characterTransform.localRotation = currentOrientation * turnRotation; // perform the look around rotation then the character-to-origin orientation rotations
}
}
``````

You do realize I only need to set the vector once for instantiate functionâ€¦

But I should be able to integrate this into my project easily. but this line:

``````Vector3 resultUp = characterTransform.position- originTransform.position;
``````

Wouldnâ€™t one normalize this vector first?

Iâ€™m aware of that. The script was strictly to show you whatâ€™s going on in Realtime and that it doesnâ€™t matter where you place the Model relative to the origin transform - the up vector will be assigned with the correct value.

LOL! :p:hushed:

It works, but I might have to reverse the position, because my characterâ€™s head points downwards!

Of course, I can fix this easily!

Then either your models follow a -Y up rule, or you mixed up the character and origin transform parameters.

Either way, happy coding!

Ok, all fixed now! thank you for all your help!