Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by PatrikSK · Mar 28, 2013 at 02:28 PM · javascriptshootingdirectiongunvector

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 28, 2013 at 02:29 PM 0
Share

For an accurate answer, please post your script.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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));

     }
     }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PatrikSK · Mar 30, 2013 at 08:58 AM 0
Share

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.

avatar image robertbu · Mar 30, 2013 at 01:06 PM 0
Share

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);
         }
     }
 }
avatar image PatrikSK · Mar 30, 2013 at 07:32 PM 0
Share

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?

avatar image robertbu · Mar 30, 2013 at 08:04 PM 0
Share

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.

avatar image PatrikSK · Mar 31, 2013 at 09:24 PM 0
Share
 //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);
 
 `
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

For some akward reason bullet is moving in the wrong direction 1 Answer

Advanced FPS shooting 1 Answer

Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer

Shooting script C# 1 Answer

How do i shoot in the direction im facing? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges