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 dudebot13 · Mar 09, 2013 at 07:45 PM · triggervariablereference-other-object

Change the FirstPersonControll Gravity Variable When Entering a Trigger

Hello,

I am creating a game where you are falling for a very long time and very quickly. I want to make it so that gravity is normal (using the pre-built first-person controller in Unity) until I jump off the edge and fall through a trigger. The trigger will then change the gravity variables and a couple others, like max fall speed. I have been trying to use this code to reference the gravity script:

 function OnTriggerEnter(Trigger : Collider) {
   if (Trigger.tag == 'GravitySpeed') {
     
     var script: CharacterMotorMovement = Trigger.GetComponent(CharacterMotorMovement);
     
     script.gravity = 1000;
   }
 }

However, I am not 100% on where to apply this script, or even if it is written correctly. It is not working right now. Can you guys give me a hand?

Thanks so much!

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 Josh707 · Mar 10, 2013 at 01:44 AM

You should have a script on the player with functions that set all of those values at once and call it from the trigger. In the script on the player, have variables for the character motor and the desired gravity, and set those in a function.

 public class newScript : MonoBehaviour { //Don't know how JS scripts start, just the name
         var playerMotor : CharacterMotorMovement
         var desiredGravity : float
         etc.
     
     
         public function setThoseValues(){ //Not sure how functions are in JS, but must be public
             playerMotor.gravity = desiredGravity;
             //Do the rest here
     }
 }


Then make an empty GameObject with a box collider scaled to the size you want, make it a trigger and call the function on trigger enter.

 function OnTriggerEnter(col: Collider){
     if(col.tag == 'Player'){
         col.GetComponent<newScript>().setThoseValues();
     }
 
 }
Comment
Add comment · Show 4 · 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 dudebot13 · Mar 10, 2013 at 03:02 PM 0
Share

This makes sense. Unfortunately, when I go in and edit the code to fit my environment, I have about 10 compiling errors, and I can never whittle them all down to 0 to actually apply them to the objects... I haven't had this many before and can't seem to get to a place where they will work. Any ideas?

avatar image Seth-Bergman · Mar 10, 2013 at 03:06 PM 0
Share

could you post the part of the (other) script where gravity is used please?

avatar image Josh707 · Mar 10, 2013 at 05:01 PM 0
Share

Huh. I really don't know anything at all about javascript, but could you post a bit of the code you wrote? I don't know why you would be getting that many errors.

avatar image dudebot13 · Mar 10, 2013 at 05:06 PM 0
Share

This is just the Character$$anonymous$$otor Script. I just need to call the Character$$anonymous$$otor$$anonymous$$ovement and change the gravity variable. So far I have tried a ton of things and have had no success.

 #pragma strict
 #pragma implicit
 #pragma downcast
 
 // Does this script currently respond to input?
 var canControl : boolean = true;
 
 var useFixedUpdate : boolean = true;
 
 // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
 // Very handy for organization!
 
 // The current global direction we want the character to move in.
 @System.NonSerialized
 var input$$anonymous$$oveDirection : Vector3 = Vector3.zero;
 
 // Is the jump button held down? We use this interface ins$$anonymous$$d of checking
 // for the jump button directly so this script can also be used by AIs.
 @System.NonSerialized
 var inputJump : boolean = false;
 
 class Character$$anonymous$$otor$$anonymous$$ovement {
     // The maximum horizontal speed when moving
     var maxForwardSpeed : float = 10.0;
     var maxSidewaysSpeed : float = 10.0;
     var maxBackwardsSpeed : float = 10.0;
     
     // Curve for multiplying speed based on slope (negative = downwards)
     var slopeSpeed$$anonymous$$ultiplier : AnimationCurve = AnimationCurve($$anonymous$$eyframe(-90, 1), $$anonymous$$eyframe(0, 1), $$anonymous$$eyframe(90, 0));
     
     // How fast does the character change speeds?  Higher is faster.
     var maxGroundAcceleration : float = 30.0;
     var maxAirAcceleration : float = 20.0;
 
     // The gravity for the character
     var gravity : float = 10.0;
     var maxFallSpeed : float = 20.0;

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

12 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

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Getting a Trigger's parent 1 Answer

making a variable (var) false when not colliding with a trigger. 1 Answer

Using variables from colliders across scripts 1 Answer

getComponent not working with Triggers? 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