- Home /
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();
}
Are you calling your Test method from anywhere? can we see the whole script?
I posted the whole script, the constant force is referenced in my "SetUnderwater" function.
Which values do you want to change?
What event do you want to take place to change the values? (Button push, Trigger area etc)
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.
If that helped, be sure to select it as the answer. It will help future readers and give you karma.
Your answer
Follow this Question
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