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 SpeedTutor · Jul 18, 2013 at 12:17 PM · javascriptfunctionconstantforce

Changing constant force component values via script?

Hello,

I've applied a "Constant force" component to my FPS controller and wondering how to access and changes some values in a function. I took this code directly from the scripting reference but doesn't seem to work.

I tried: (EDITED)

     #pragma strict
 
 var waterLevel : float;
 private var isUnderwater : boolean;
 private var normalColor : Color;
 private var underwaterColor : Color;
 //var constantForce : ConstantForce;
 
 var myParticles : ParticleSystem;
 private var chMotor : CharacterMotor;
 
 function Start () 
     {
         normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
         underwaterColor = new Color (0.22f, 0.65f, 0.77f, 0.5f);
         chMotor = GetComponent(CharacterMotor);
         myParticles.Stop();
     }
 
 function Update () 
     {
             if ((transform.position.y < waterLevel) != isUnderwater) 
             {
                 isUnderwater = transform.position.y < waterLevel;
                 if (isUnderwater) SetUnderwater ();
                 if (!isUnderwater) SetNormal ();
             }
             if(Camera.main.transform.rotation.x < 10)
             {
                 Debug.Log ("Down");
             }
             
             if(Camera.main.transform.rotation.x >= 10)
             {
                 Debug.Log ("Up");
             }
         }
 
 function SetNormal () 
         {
             RenderSettings.fogColor = normalColor;
             RenderSettings.fogDensity = 0.5f;
         }
      
 function SetUnderwater () 
         {
             RenderSettings.fogColor = underwaterColor;
             RenderSettings.fogDensity = 0.03f;
             //constantForce.relativeForce = Vector3(0, 0, 1);
             chMotor.movement.gravity = 2;
             chMotor.movement.maxFallSpeed = 5;
             chMotor.movement.maxForwardSpeed = 4;
             chMotor.movement.maxSidewaysSpeed = 4;
              myParticles.Play();
         }
         
    
      
Comment
Add comment · Show 3
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 amphoterik · Jul 18, 2013 at 12:19 PM 0
Share

Are you calling your Test method from anywhere? can we see the whole script?

avatar image SpeedTutor · Jul 18, 2013 at 12:24 PM 0
Share

I posted the whole script, the constant force is referenced in my "SetUnderwater" function.

avatar image andytouch · Jul 18, 2013 at 12:28 PM 0
Share

Which values do you want to change?

What event do you want to take place to change the values? (Button push, Trigger area etc)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by amphoterik · Jul 18, 2013 at 12:30 PM

Assuming you have added the constant force component via the Unity Editor AND you have a rigidbody on the character, then you are close. This line:

 //var constantForce : ConstantForce;

Can go away because you already have a constant force. Then you simply need to do:

 constantForce.relativeForce = Vector3(0, 0, 1);

Like you are already doing. The variable constantForce will already exist because the component is on the object.

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 SpeedTutor · Jul 19, 2013 at 12:24 AM 0
Share

Cheers man, great help!

avatar image amphoterik · Jul 19, 2013 at 12:30 AM 0
Share

If that helped, be sure to select it as the answer. It will help future readers and give you karma.

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

17 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

Related Questions

How to use functions between two scripts 2 Answers

Restart this script 3 Answers

Camera Move 1 Answer

How to call a function from a script in another scene 5 Answers

Player Health drops fast, how to pause after damage 0 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