- Home /
How to control the speed of the projectile motion?
I have created a simple script which will move the sphere in a projectile motion.
public class ProjectileTest : MonoBehaviour
{
public float power;
public float speed = 1f;
void OnGUI ()
{
if(GUI.Button(new Rect(100, 100, 100, 50), "Reset"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
}
if (Input.GetMouseButton (0))
{
power+=0.1f;
}
if (Input.GetMouseButtonUp (0))
{
Debug.Log("Power : "+power);
if(power > 10)
power = 10f;
transform.rigidbody.isKinematic = false;
Vector3 forceVector = new Vector3(1, 1, 0);
transform.rigidbody.velocity = forceVector * power * speed;
}
}
}
Now as the question says, how can i control the speed of the projectile motion?
The sphere should move in the projectile but with a factor of speed.
Like sometimes i want it to move fast or slow but the projectile path should be same.
Answer by robertbu · May 17, 2014 at 06:17 AM
To change the speed, simply change the 'speed' or 'power' variable. But that does not get what you specify in the latter half of your statements. Having a projectile follow the path whether the projectile is fast or slow, breaks physics. The only way I can see with involve some somewhat complicated math and varying the gravity on a throw-by-throw basis. You could also abandon the use of the Rigidbody, and calculate the path directly and move the projectile using the Transform. A sine function makes a nice arc.
so with the current method i cannot control the speed of the projectile path?
so with the current method i cannot control the speed of the projectile path?
Not 100% true, but not easily solved either. The Rigidbody component uses the physics engine. This is an approximate simulation of real world physics. Imagine in the real world you were throwing a ball at some specified angle and it is hitting a target. If you slow down your throw the ball will fall short. If you speed up your throw, you will overshoot the target. There are a couple things that can be done. You can change the time scale as indicated by @Bored$$anonymous$$ormon, or you can change Physics.gravity, but I doubt either solution is one you are looking for.
If your criteria is a must, you will need to look to an alternate solution to a Rigidbody for your movement.