Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DudiEjje · Oct 03, 2015 at 02:19 AM · change variable

Is there a possible way to make gravity change when the character collides with something?

I would like the gravity to change when the character collides with an empty object.

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 Suddoha · Oct 03, 2015 at 02:27 AM

Yes, you have access to the gravity via the Physics class. If the "empty object" is supposed to be an area rather than a certain point in space, you should at least have a collider set to isTrigger that you can run into, so that you can use the OnTriggerEnter(Collider other) or a normal collider so that you can use OnCollisionEnter(Collision other) and execute the actions you want to happen.

Changing the gravity is just one line of code (of course you'll probably have more logic going on, but that's how to change it):

3D:

 Physics.gravity = new Vector3(x,y,z); 

2D:

  Physics2D.gravity = new Vector2(x,y);

Basic example using trigger:

 void OnTriggerEnter(Collider other)
 {
     // do some stuff
     if(other.CompareTag("theTagForObjectsThatChangeGravity"))
         Physics.gravity = new Vector3(x,y,z);
 }

with x,y,z being the desired values for the new gravity.

Comment
Add comment · Show 3 · 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 DudiEjje · Oct 06, 2015 at 01:17 AM 0
Share

I'm still not able to do it. I changed the ideia and i would like the object to be a sprite, ins$$anonymous$$d of an empty object. I would like to, when the player pass through the sprite, add a certain ammount of gravity . In the case, i would like to work with OnTriggerEnter.How can i do it? I'm having trouble in this part!

avatar image Eno-Khaon DudiEjje · Oct 06, 2015 at 02:18 AM 1
Share

If you're working with 3D, you would use:

 // C#
 void OnTriggerEnter(Collider other)
 {
 }

whereas in 2D, you would use:

 // C#
 void OnTriggerEnter2D(Collider2D other)
 {
 }

The documentation (linked for each version) describes the requirements for their usage. To quote the 3D implementation, "Trigger events are only sent if one of the colliders also has a rigidbody attached." -- So make sure at least one object has a Collider with trigger enabled and the other has at least a Rigidbody.

avatar image DudiEjje Eno-Khaon · Oct 07, 2015 at 01:30 AM 0
Share

I did it! Thanks! But i have a final question. What would be the lines of code so i can put other events to happen, besides that?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

changing maxSidewaysSpeed in a script 0 Answers

Is there a good way to change a variable from a child object of the object that collided with a trigger, without giving a reference beforehand? 3 Answers

Change the value of the motorspeed 2 Answers

When Movement speed Change once it never change again 1 Answer

Change a variable on a prefab in unity 5 js 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