Hello, I am trying to make a basic little game. However, my character needs to move around using local AddForce. I had a look around, and I found that the best way to go is AddRelativeForce.
My code;
rigidbody.AddRelativeForce(Vector3.up * forceUp);
However, this does not do anything. It sort of moves the object sideways, but not really.
Help? I don’t know whats wrong.
When I added the code to a cube, it worked fine, but not with my character. Main script:
#pragma strict
var b_thruster : ParticleSystem;
var l_thruster : ParticleSystem;
var r_thruster : ParticleSystem;
var top_thruster : ParticleSystem;
var forceUp : float = 10;
var forceSide : float = 10;
var explosion : GameObject;
var guiShow : boolean;
private var lander : GameObject;
var boost : float = 20;
function Start()
{
lander = this.gameObject;
lander.renderer.material.color = Color.grey; // or whatever
}
function Update()
{
r_thruster.particleSystem.Stop();
l_thruster.particleSystem.Stop();
if(Input.GetAxis("Vertical") > 0) //Checking for up arrow
{
rigidbody.AddRelativeForce(Vector3.up * forceUp); //Problem line
b_thruster.particleSystem.Emit(1);
}
if(Input.GetAxis("Vertical") == 0) //Checking for no vertical keys
{
top_thruster.particleSystem.Stop();
b_thruster.particleSystem.Stop();
}
if(transform.position.z > 10) //Checking if inside bounds (R)
{
Debug.Log("Outside of bounds! Outside of bounds!");
transform.position.z = 10;
}
if(transform.position.z < -10) //Checking if inside bounds (L)
{
Debug.Log("Outside of bounds! Outside of bounds!");
transform.position.z = -10;
}
}
function Explode()
{
//var rand : int;
//rand = Random.Range(0,explosion.length);
Instantiate(explosion, transform.position, Quaternion.identity);
lander.renderer.material.color = Color.red; // or whatever
Destroy (constantForce);
Destroy (rigidbody);
guiShow = true;
}
function OnGUI()
{
if(guiShow)
{
if (GUI.Button (Rect (30,70,500,30), "You lost! Restart game?"))
{
Application.LoadLevel ("scene1");
}
}
}
function OnCollisionEnter(hitInfo : Collision)
{
if(hitInfo.relativeVelocity.magnitude >= 1.5)
{
Explode();
}
}
function RemoveRigid(on : boolean)
{
if(on)
{
Destroy(constantForce);
Destroy(rigidbody);
} else {
//Add the same customized rigidbody
lander.AddComponent("Rigidbody"); // Add the rigidbody.
lander.rigidbody.useGravity = false;
lander.rigidbody.drag = 0.9;
lander.rigidbody.angularDrag = 0.05;
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationY;
rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
//Add the constantforce
lander.AddComponent("ConstantForce");
constantForce.force = Vector3(0,-5,0);
//Boost
}
}