How to rotate an empty obj around (0,0,0) at a fixed distance based on mouse position.
Title basically says it all. For some context, I'm trying to recreate the Disintegrate skill from Diablo III. It functions by planting the character (or 0,0,0 for me) and producing a beam in the direction of your mouse, at a constant distance.
So far, I can do pretty much everything outside of having a fixed distance. I set a point on a line renderer to my "player" and another to an empty that is controlled by my mouse. However, I can currently bring the empty, and therefore the end of the beam, as close or far away from the origin as I wish. Is there a way to like, clamp it at 10 units away from the center at all times?
I also have been able to clamp the empty at a set distance and rotate it around the center by using RotateAround(Vector3.zero, Vector3.up, 1);
but then I can't have it locked to the mouse, instead it always rotates.
Any tips or resources I should check would we super helpful, thanks guys!
Answer by mguidry · Nov 24, 2018 at 02:41 AM
If I am understanding you correctly since the origin is <0,0,0> you should just be able to take the position, normalize it, then multiply it by the distance you want.
transform.position = transform.position.normalized * desiredDistance;
Yes, that worked for me! Thank you.
Can't say I exactly understand it, but now I know to look into how normalizing a transform functions.
Thanks again!