Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by IgnitedGames · Feb 02, 2020 at 01:10 AM · vector3gravitymath

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgnitedGames · Feb 02, 2020 at 06:03 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgnitedGames · Feb 02, 2020 at 08:07 PM 0
Share

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.

avatar image
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 :))

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Change player gravity by pressing a key? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges