Get a Vector3 to always have a radius of 1 from 0, 0, 0
Basically say I have a Vector3. I want to make it so that if the origin is and always is 0, 0, 0, how I can get it so the Vector3 will always be a radius of 1 away from that point, but is still pointing in the same direction toward 0, 0, 0.
I'm pretty sure the way you would do this in a 2D plane is to draw a circle around 0, 0 and figure out the radius of that, then divide both the X and Y by that radius. I haven't tested that but it sounds like that is the way to go. I don't know how I would do that in a 3D plane though.
I'd imagine this is gonna be really complicated and the math this requires is gonna be crazy, but I'd imagine it's also just a copy-and-paste situation. Don't feel like you need to explain why it works if you don't want to.
In case you're wondering why I need this, I'm trying to create a mechanic that makes the Physics.gravity based on the player's collision's hit point and the transform of the player. The problem is despite my efforts the gravity keeps on getting set too strong, I just want to have this ran every few frames. If there's a better way to do it than this, I'd prefer that solution instead if you'd be willing to tell me!
I'd really appreciate any help, thank you for reading!
Okay, I've figured out a really ill-optimised temporary solution, here's the code:
GameObject emptyGravity = Instantiate(new GameObject(), temp01, new Quaternion()); emptyGravity.transform.LookAt(Vector3.zero); while (!$$anonymous$$athf.Approximately(Vector3.Distance(Vector3.zero, emptyGravity.transform.position), 1)) { if (Vector3.Distance(Vector3.zero, emptyGravity.transform.position) > 1) { emptyGravity.transform.Translate(emptyGravity.transform.forward / 100); } else if (Vector3.Distance(Vector3.zero, emptyGravity.transform.position) < 1) { emptyGravity.transform.Translate(-(emptyGravity.transform.forward / 100)); } } Physics.gravity = emptyGravity.transform.position; Destroy(emptyGravity);The Vector3 that I want turned into a Vector3 with a radius of 1 from Vector3.zero. It seems to work pretty well, but I'd imagine it is incredibly unoptomised. Do not put this in an Update loop, this is only ran in my code when the function is called, which is only in specific scenarios. When I ran this code additionally:
Debug.Log(Vector3.Distance(emptyGravity.transform.position, Vector3.zero)I practically only got "1" out in the console, so I'm gonna assume that it is working properly.
Answer by tormentoarmagedoom · Feb 02, 2020 at 06:16 PM
Hello are you just trying to determinate a "gravity" direction?
You know there is the LookAt funciton and the Normalize function?
Bye!
I already figured that out, I’m just trying to make it so that the point, relative to 0,0,0, would lie on the edge of a sphere with a radius of 1 and a centre at 0,0,0.
Answer by Captain_Pineapple · Feb 02, 2020 at 09:21 PM
Hey, well in general tormentoarmagedoom is right. There is the function Vector3.Normalize(YOURVECTORHERE);
which will return the vector you feed in with a length of 1. This is also nothing really "fancy or crazy math" as you described it. Well at least as long as you've heard of the theorem of Pythagoras that is. But nonetheless just use the function provided by unity.
in any case there is also the following possibility: Assume you have a Vector3 testVector
then you can always access the normalized version by testVector.normalized
.
this basically gives you a given direction with a vector length of 1, so the given point is basically on a sphere with radius 1 around the origin. (even though this description of yours is a bit fancy :))
Your answer
Follow this Question
Related Questions
Bring 3 Vector 3s together 1 Answer
How can I bind this script into the state of my camera? 2 Answers
Unity plane constructor bug? 1 Answer
Gravity To CharacterController 0 Answers