- Home /
Why is my gun shooting backward?
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
Answer by PatrikSK · Mar 28, 2013 at 03:33 PM
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));
}
}
}
Answer by robertbu · Mar 28, 2013 at 03:38 PM
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.
Ok i remember. I tryed to use your solution but the result is still the same. The object moves and the bullet shoots backward. I look over the object if the bullet dont colides with something, but i cant find anything.
The following code is yours with two changes. It will fire in the direction the game object you have this attached to is facing. Forward is the side facing positive 'z' when the game object has a rotation of (0,0,0).
public var bulletPrefab : Transform;
public var bulletSpeed : float = 6000;
function Update() {
if (Input.GetButtonDown("Fire3")) {
if (!bulletPrefab) {
Debug.Log("bulletPrefab undefined");
}
else {
var bulletCreate = Instantiate(bulletPrefab, transform.position, transform.rotation);
bulletCreate.rigidbody.AddForce(transform.forward * 1000.0);
}
}
}
I tryed that before and now again with your solution. Unfortunately is the result still the same. I think that can by a conflict between the object movement script and the child object used as gun with that bullet script. Or?
This script will shoot in the direction of the object it is attached to, so highlight that object in the inspector and take a look at the rotation as you play. I'm not sure how you have your scene setup nor do I know what you have this script attached to. Typically the object this script is attached to would either 1) be a child object of whatever was aimed, or 2) would have a transform.LookAt() to point at a specific target.
//Turn variables to rotation and position of the object
rotationx=transform.eulerAngles.x;
rotationy=transform.eulerAngles.y;
rotationz=transform.eulerAngles.z;
positionx=transform.position.x;
positiony=transform.position.y;
positionz=transform.position.z;
//Speed
transform.Translate(0,0,speed/5*Time.deltaTime);
`