- Home /
How to speed up projectile thrown?
I'm trying to throw a dagger by adding force base on vector drawn by holding mouse. Everything worked fine, only problem is the dagger took too long time to fly, even a short distance with small force took about 4, 5 seconds. Is there any solution for this or am I doing something wrong? Here's the code:
void Update()
{
// Get mouse positiom
Vector3 mousePos = Input.mousePosition;
mousePos.z = 200;
Vector3 mouseWorldPos = Camera.main.WorldToScreenPoint(mousePos);
if(Input.GetMouseButtonDown(0) && ProjectileLauched == false){
IsAiming = true;
// Check if this position is the beginning of drag
if(beginDrag == false){
startPos = mouseWorldPos;
beginDrag = true;
}
} else if(Input.GetMouseButtonUp(0) && IsAiming && ProjectileLauched == false){
endPos = mouseWorldPos;
beginDrag = false;
if(checkValidDrag()){
IsAiming = false;
ProjectileLauched = true;
Vector3 forceDrawn = endPos - startPos;
forceDrawn.z = mousePos.z;
Debug.Log(forceDrawn);
ActiveWeapon.GetComponent<Rigidbody>().useGravity = true;
ActiveWeapon.GetComponent<Rigidbody>().AddForce(forceDrawn * 0.7f, ForceMode.Impulse);
}
}
// Rotation on hold
if(beginDrag == true){
Vector3 vectorDrawn = mouseWorldPos - startPos;
Quaternion rotation = Quaternion.LookRotation(vectorDrawn, Vector3.forward);
ActiveWeapon.transform.rotation = rotation;
}
}
Answer by CodesCove · Aug 03, 2020 at 10:38 PM
I don't know if this helps but here are few thing that that can affect the speed
Rigidody Mass value. Higher mass value causes slower speed in the initial impulse and vice versa (notice that mass!=weight).
Rigidody Drag. Higher value then the objects speed decelerates faster and vice versa
Rigidody Any active Constraints that might prevent movement
Magnitude of .AddForce force vector. The larger the magnitude the higher the initial speed is
Direction of the .AddForce force vector especially if the useGravity is enabled. If you shoot against the Physics gravity vector then the speed is decelerated until object comes down (you can set it in the Edit -> Project Settings -> Physics --> Gravity)
Time.timeScale value that is affects all the motion. If lower than 1 then the motion (and almost whole game except frame rate depended parts) is slowed down.
This might be something that is done intentionally but just make sure.. In your code you take the direction but then set the direction vectors z-value to mousePos.z = 200. That value is included to both the direction and to the magnitude of the force vector that is applied to the projectile. If you want pure direction then you get from forceDrawn.normalized property and then multiply that with force value to get the real force vector (that includes direction and force). Hope you get it working!
I just realize I scaled my objects too big so when I throw, it's like lauching a rocket in thousand kilometers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004828im_/https://answers.unity.com/themes/thub/images/avi.jpg)