- Home /
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.
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.
}
}
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:
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?
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"?
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.
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.
Your answer
Follow this Question
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