- Home /
AddForce to sphere
I have a sphere with rigid body, (Is Kinematic = False) and Configurable Joint (Configured in World Space = True); using AddForce(0, 500, 0). The sphere's movement still depends on it's rotation.
#pragma strict
var moveSpeedX = 15;
var maxSpeedX = 15;
var baseSpeed = 0;
var jumpForce1 = 450.00;
var jumpForce2 = 200.00;
private var doubleJump : boolean = true;
private var grounded : boolean = false;
function Update()
{
if(Physics.Raycast(transform.position, -transform.up, 1)) {
grounded = true;
doubleJump = true;
} else
grounded = false;
if(Input.GetButtonDown("Jump")) {
if(grounded) {
rigidbody.AddForce(0, jumpForce1, 0);
}
else if(doubleJump) {
doubleJump = false;
transform.rigidbody.AddForce(0, jumpForce2, 0);
}
}
if(Input.GetKey(KeyCode.LeftArrow)) {
if (transform.rigidbody.velocity.x > -maxSpeedX) {
transform.rigidbody.velocity.x = transform.rigidbody.velocity.x - (2*moveSpeedX) *Time.deltaTime;
} else {
transform.rigidbody.velocity.x = -maxSpeedX;
}
}
if(Input.GetKey(KeyCode.RightArrow)) {
if (transform.rigidbody.velocity.x < maxSpeedX) {
transform.rigidbody.velocity.x = transform.rigidbody.velocity.x + (2*moveSpeedX) *Time.deltaTime;
} else {
transform.rigidbody.velocity.x = maxSpeedX;
}
}
}
Also tried using: rigidbody.AddForceAtPosition(myForward * 1000.0, rigidbody.worldCenterOfMass); That didn't work either. Sorry for question that a million noobs like me have already asked, but I can't get it to work.
Answer by JanWosnitza · Oct 03, 2012 at 08:08 AM
rigidbody.AddForce(..) should work as expected. But your raycast should look like this:
if(Physics.Raycast(transform.position, -Vector3.up, 1))
I also recommend you to use:
rigidbody.AddForce(0, jumpForce1, 0, ForceMode.VelocityChange);
rigidbody.AddForce(0, jumpForce2, 0, ForceMode.VelocityChange)
so you can set the speed independently from the sphere's previous velocity (like SuperMario does) and the sphere's mass. Thus you must tweak your jumpForces again.
good luck ;)
For some reason it didn't move with Force$$anonymous$$ode.VelocityChange no matter how I set highjumpForce1, but it seems to work now with: rigidbody.AddForce(Vector3.up*jumpForce1, Force$$anonymous$$ode.Acceleration); Thank you.
Your welcome. $$anonymous$$aybe Force$$anonymous$$ode.Impulse was the better choice since Force$$anonymous$$ode.Acceleration is for accelerations (appling a force in every FixedUpdate ^^ e.g. custom gravity for diffent objects)