Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by kaladin84 · Mar 23, 2014 at 10:44 AM · gravityforce

Custom Gravity issues

I am attempting to apply custom gravity to all objects in my game. to start with all the objects should have standard (0,9,0) gravity, but as the game goes on the player has the ability to set a new 'Down' direction for themselves or any item/other player

so far I have

public class GravityScript : MonoBehaviour { public Vector3 GravityForce; public Vector3 tempGrav; Vector3 newUp; Vector3 CoG; // Use this for initialization void Start () { this.rigidbody.useGravity = false; GravityForce = Physics.gravity; CoG= this.transform.position+this.transform.up; Vector3 newUp = this.transform.up; } // Update is called once per frame void Update () { CoG= this.transform.position+this.transform.up; if(Input.GetButtonDown("Fire1")) { RaycastHit hit=new RaycastHit(); Ray ray = new Ray(CoG, Camera.main.transform.forward); if(Physics.Raycast(ray, out hit)) { tempGrav = hit.normal *-10; // tempGrav = tGrav; newUp = hit.normal; } } if(tempGrav!=GravityForce&&Input.GetButtonUp("Fire1")) { GravityForce = tempGrav; } } void FixedUpdate() { rigidbody.AddForce( GravityForce * Time.fixedDeltaTime, ForceMode.Acceleration); } }

it is not a simple case of inverting gravity, as it could be assigned to an angular surface. The script above only applies for modifying there own gravity, well it should I cannot seem to get it to work.

any ideas - I know I have to implement selfrighting as well - pointers on that would be good too!

Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fornoreason1000 · Mar 23, 2014 at 11:14 AM

how does your custom gravity work? are you using rigigbodies or id you make a script?

if script you can make a child script from it, then give a public function that changes the direction of gravity.

if rigid bodies you could turnoff gravity and rotate the gravity vector (Physics.Gravity, store it in another variable!) to the direction the player sets it.

This code serves as a guide, copying paste pasting it will probably cause all sorts of weird things(e.g character slowly being pulled through the floor, jitterbugging, poor collisions)

 Vector3 grav
 
 public void ApplyMyGravity() {
 
 
 
 //apply manual gravity
 transform.position =- grav * Time.Deltatime
 
 }
 
 public void SetMyGrav(Vector3 dir) {
 
  grav = Physics.gravity * Quaternion.Euler(dir);
 
 
 }
 
 

Hope it helps

Comment
Add comment · Show 2 · 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 kaladin84 · Mar 23, 2014 at 07:12 PM 0
Share

I wanted a script that I could attach to any object (including FPS player) so all gravity for that item, falling etc is based on the new vector. I managed to get it working a bit but I had to put the multiplier up to -100. Which isn't right.

avatar image Fornoreason1000 · May 26, 2014 at 05:41 AM 0
Share

please post comments as comments

the code as i said serves as a guide, I can't write scripts for you, i can only tell you how to write them yourself and to guide you when you get stuck writing to get you going again.

as for the multiplier. Its sounds like its conflicted with other movements in another script probably your FPS Player

this code actually goes inside FPS player, you know where all the physic logic is...if you don't it will just conflict each other causing glitches.

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

21 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

Related Questions

Setting limits to my trajectory 2 Answers

How Do I Increase Gravity Overtime If My Player Stays In The Air Longer? 1 Answer

how to let an object drive on a road? 1 Answer

Keeping character's movement consistent. 1 Answer

adding force every frame to control my ridigbody makes it shake ugly 4 Answers


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