# How to orient entity rotation towards center (0,0,0)?

Can you suggest a way to calculate the local rotation needed to make an object @ some (x,y,z) orient towards the center (0,0,0)?

My attempt below is funky haha; if I omit the “angleZ”, it appears to work but only in one quadrant of the sphere. I used the “raycast” method to calculate the normal vector from sphere center (0,0,0) to the sphere surface that has the entity (x,y,z), and then Math.atan to figure out the corresponding rotation angles. I suspect my math logic is way wrong - has anyone figured out these calculations before?

``````var AutoAlign = pc.createScript('autoAlign');

// initialize code called once per entity
AutoAlign.prototype.initialize = function() {
var from = new pc.Vec3(0,0,0);
var to = this.entity.getPosition().clone();
var result = this.app.systems.rigidbody.raycastFirst(from, to);

this.dire = result;

var angleX = Math.atan(this.dire.normal.z / this.dire.normal.y) * (180 / Math.PI);
var angleY = Math.atan(this.dire.normal.x / this.dire.normal.z) * (180 / Math.PI);
var angleZ = Math.atan(this.dire.normal.y / this.dire.normal.x) * (180 / Math.PI);
this.entity.setLocalRotation(new pc.Quat().setFromEulerAngles(angleX,angleY,angleZ));

//console.log(this.dire);
//console.log(angleX + ' ' + angleY + ' ' + angleZ);
};
``````

*if easier to look at code directly, this is being used in my test game