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 lvlup · Jan 23, 2011 at 03:19 PM · forceconstantforce

how do i write this: i want to get the Constant Force Force Z value from this object using this script, so i can use it in another script?

i'm trying to write,

i have the Component, Constant Force, with Force Z set to a value, in my first script, that is attached to my first gameobject, named vehicle.

i now want another gameobject, named booster, OnTriggerEnter, to increase that Force being applied on "vehicle" object.

you can see my current code here: http://forum.unity3d.com/threads/75076-how-to-GetComponent-constantForce.force-from-another-script

something like this sounds so straightforward but i just burnt through another day trying to write this out, referencing unity's manual, forums and search, unity wiki, youtube videos, and googling.

gotta get this working to move on with work!

[what im trying to do] im trying to make a "boat" object increase or decrease its forward speed when it "hits" or goes over a powerup 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
1
Best Answer

Answer by Peter G · Jan 23, 2011 at 03:53 PM

Personally, I would avoid posting in 2 places because there will likely be a lot of redundancy between the two threads.

Let me give you some pointers so you can get this working. I'm giving you the completed code so that you can get back to work with the expectation that you will read it and understand the changes.

//Engine.cs using UnityEngine; using System.Collections;

public class Engine : MonoBehaviour { //Common coding convention is for classes to be begin with a Capital letter i.e. Engine

 public int speed = 10;

 //You can remove the Start() function if you don't use it, it will be called even if it is empty.


 // Update is called once per frame
 void Update () {
     //Unity has a built-in input system for axises that will simplify your code,
     //and you probably don't want to use GetKeyDown because that will only return true
     //on the first frame, and there is no sensitivity.

     transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * speed);    
     //This 1 line does everything your old system did.            

     if (Input.GetKeyDown("x")){
         //You probably want Input.GetKey() because that returns every frame.
         print("X was pressed");
         constantForce.force = Vector3.forward * 1000;
     }
 }

}

And your trigger class:

using UnityEngine; using System.Collections;

public class Booster : MonoBehaviour {

  public GameObject target;

  void OnTriggerEnter () {
       //If you don't need the parameter, don't include it.
       //It saves memory.
       Boost();      
  }

  private void Boost(){
       engine e = target.GetComponent<engine>();
       //You don't need a cast, that the whole point of generics.
       //I don't think you need this either.

       ConstantForce motor = target.GetComponent<ConstantForce>();
       //You needed an instance.
       motor.force = Vector3.forward * 3000;
       //Hard-coded values are generally not good.
  }

}

Comment
Add comment · Show 10 · 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 lvlup · Jan 24, 2011 at 02:28 AM 0
Share

Heya Peter

thanks for takin the time to do that!

i've not used the code yet, or tested it yet, still trying to understand what you did.

i think the part that was driving me nuts to get a grip on was the GetComponent(), i used this page (http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html) prior to posting, to $$anonymous$$ch myself, but i just dont get it, and now that i see what you wrote ins$$anonymous$$d, i'm still as confused how that works.

so is ConstantForce and constantForce the same thing? o:

avatar image lvlup · Jan 24, 2011 at 02:41 AM 0
Share

oh and for the boost class,

theres no Update or Start, how come, what deter$$anonymous$$es when i should or should not use an Update? i would think that we want unity to be always checking "every frame" whether or not the trigger is activated?

avatar image lvlup · Jan 24, 2011 at 02:42 AM 0
Share

and and why do we not need a parameter for the OnTriggerEnter, don't we need to tell it what to check for that has "entered" it? in which case, the collider mesh of the "boat object"?

avatar image Peter G · Jan 24, 2011 at 11:51 PM 0
Share
  1. constantForce and ConstantForce are not the same thing. I don't know how much C# experience you have, but it is case-sensitive. ConstantForce is a class. constantForce does not exist unless you define it. All's you are doing is defining a variable "e" that is of class "engine", and it is referencing the engine script attached to the "target" GameObject. GetComponent() returns the requested component already cast.

avatar image Peter G · Jan 24, 2011 at 11:52 PM 0
Share
  1. Unity internally keeps track of all collisions and triggers. It will automatically call OnTriggerEnter() on any object when it enters a trigger so you don't need to tell it to check manually.

Show more comments

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

No one has followed this question yet.

Related Questions

How not to lose speed when collided with wall? 2 Answers

Constant force question 1 Answer

Constant Force Script Changing 1 Answer

How to implement Mob movement / death ? 1 Answer

AddForce to object 3 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