- Home /
ForceMode.Acceleration
Hello,
I'm trying to get an object to accelerate and increase speed, and it doesn't look like the object is doing that. Here's my code:
using UnityEngine;
using System.Collections;
public class Acceleration : MonoBehaviour {
public float ForceSpeed;
// Update is called once per frame
void FixedUpdate () {
rigidbody.AddForce(Vector3.right * Time.deltaTime * ForceSpeed, ForceMode.Acceleration);
print(ForceSpeed);
}
}
The object looks like it's moving at the same speed. What do I need to do? Also, how do I print 'ForceSpeed' every three seconds? I've checked online and I still can't seem to get it. Any help is appreciated. Thank you for your time.
I figured out how to print the 'ForceSpeed' variable to the console every three seconds. $$anonymous$$y object isn't accelerating. It's just maintaining the same speed. What is wrong with my code?
Answer by aldonaletto · Nov 12, 2012 at 12:15 PM
This script should work, but the values of ForceSpeed and rigidbody.drag may sabotage it! You're multiplying ForceSpeed by Time.deltaTime, which in FixedUpdate means a resulting acceleration 50 times lower than expected. Besides this, rigidbody.drag creates a friction proportional to the speed, what limits the rigidbody velocity.
Remove the multiplication by Time.deltaTime, and set Drag to zero. If you still can't get the desired result, use ForceMode.Force: a force applied in this mode in FixedUpdate results in a constant acceleration equal to force/mass. If you want an acceleration independent of the rigidbody mass, multiply ForceSpeed by rigidbody.mass:
void FixedUpdate () {
// ForceMode.Force is default, thus you don't need to specify it:
rigidbody.AddForce(Vector3.right * rigidbody.mass * ForceSpeed);
}
Why are you printing ForceSpeed? Isn't it constant? If you want to monitor the rigidbody speed, print rigidbody.velocity.ToString() instead.