My gun is attached to a moving object. I tryd more than 10 different scripts. Raycast works fine, but rigidbody based shooting always shoots backward. I tryd to mess around with camera to world point. Without sucess. Any help? Thanks - js. based game
The reason i dont posted a script is, that any script i use for the gun makes the same with the bullet shooting direction (shoots backward). So i deduct that i dont have a gun script problem and i make the mistake elsewhere. I think it can be between the parent object movement and the bullet movement. The object moves in the z direction. And this is the bullet script:
public var bulletPrefab : Transform;
public var bulletSpeed : float = 6000;
function Update() {
if (Input.GetButtonDown(“Fire3”)) {
if (!bulletPrefab || !bulletSpeed) {
Debug.Log(“[Shoot] bulletPrefab or bulletSpeed is undefined”);
} else {
var bulletCreate = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
bulletCreate.rigidbody.AddForce(new Vector3(Screen.width, Screen.height , 0));
}
}
}
Please format your code. Select the code and use the 101/010 button to mark it as code. As for your question, your problem is in this line:
bulletCreate.rigidbody.AddForce(new Vector3(Screen.width, Screen.height , 0));
You need to add the force relative to the forward of the moving object. Something like:
bulletCreate.rigidbody.AddForce(transform.forward * 1000.0);
Also, answers fields are for answers to your question. By putting your code in an answer field, your question is now marked as having two answers and is far less likely to get more. You can convert your answer to a comment using the “more” dropdown, or you can edit your original question to include the code, and delete the answer.