- Home /
Question by
Buretto · Apr 14, 2015 at 12:57 AM ·
constantforce
How to use the ConstantForce component?
I am having trouble with this error.
Assets/Underwater.js(46,47): BCE0020: An instance of type 'UnityEngine.ConstantForce' is required to access non static member 'relativeForce'. Which is on this line.
ConstantForce.relativeForce = new Vector3(0, -200, 0);
I am totally confused.
#pragma strict
public var waterLevel : float;
private var isUnderwater : boolean;
private var normalColor : Color;
private var underwaterColor : Color;
private var swimDirection = Vector3.zero;
private var chMotor : PlayerMovement;
private var chMotorscript : CharacterMotor;
public var relativeForce: Vector3;
//public var scriptName : GameObject;
//public var Player : Transform;
function Start () {
normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
underwaterColor = new Color (0.3f, 0.4f, 0.43f, 9f);
chMotor = GetComponent(PlayerMovement);
chMotorscript = GetComponent(CharacterMotor);
}
function Update () {
if((transform.position.y < waterLevel) != isUnderwater);
{
isUnderwater = transform.position.y < waterLevel;
if (isUnderwater) SetUnderwater ();
if (!isUnderwater) SetNormal();
}
if(isUnderwater && Input.GetKey(KeyCode.E))
{
ConstantForce.relativeForce = new Vector3(0, -200, 0);
}
else
{
ConstantForce.relativeForce = new Vector3(0, 0, 0);
}
if(isUnderwater && Input.GetKey(KeyCode.Q))
{
ConstantForce.relativeForce = new Vector3(0, 200, 0);
}
}
function SetNormal()
{
RenderSettings.fogColor = normalColor;
RenderSettings.fogDensity = 0.002f;
RenderSettings.fogEndDistance = 500;
}
function SetUnderwater()
{
RenderSettings.fogColor = underwaterColor;
RenderSettings.fogDensity = 0.04f;
RenderSettings.fogEndDistance = 20;
Debug.Log("hi");
chMotor.walkSpeed = 2;
chMotor.runSpeed = 3;
chMotor.enableCrouch = false;
chMotor.gravity = 5;
chMotor.enableFallingDamage = false;
//chMotor.slideWhenOverSlopeLimit = false;
}
Comment
Best Answer
Answer by DoTA_KAMIKADzE · Apr 14, 2015 at 12:59 AM
As an error says - you need an instance of ConstantForce. Add ConstantForce component to your object either via Inspector or script. And then:
//You can use GetComponent
private ConstantForce waterForce;
private void Awake()
{
waterForce = GetComponent<ConstantForce>();
}
//or
public ConstantForce waterForce; //and assign it in Inspector
//or
[SerializeField]
private ConstantForce waterForce; //and assign in Inspector
//or provide it from another script
//then use it like:
waterForce.relativeForce = new Vector3();//your vector
Your answer
![](https://koobas.hobune.stream/wayback/20220613192921im_/https://answers.unity.com/themes/thub/images/avi.jpg)