Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ThatGuyBrian · Oct 17, 2016 at 06:48 PM · instantiateraycastingbullet

Bullets are firing in the wrong direction.

I'm back at it again with another problem. This time, it's about bullets and raycasting. My bullets are only firing in one direction and I have no idea as to how to make it relative to the player's rotation. This seems like a pretty easy thing to fix, but I just can't come up with an idea as to how to fix it. Here's the code, props to anyone who can lend me a hand.

 `using UnityEngine;
 
     using System.Collections;
     
         public class BulletMovement : MonoBehaviour
         {
         
             public int speed = 20;
             float timer = 20f;
         
             // Use this for initialization
             void Start()
             {
         
             }
         
             // Update is called once per frame
             void Update()
             {
         
                 transform.Translate(Vector3.back * 20 * Time.deltaTime);
                 if (Camera.main.WorldToViewportPoint(this.transform.position).y > 1)
                 {
                     Destroy(this.gameObject);
                 }
             }
         
             void OnTriggerEnter(Collider other)
             {
                 if (other.gameObject.CompareTag("Enemy"))
                 {
                     Destroy(other.gameObject);
                     Destroy(this.gameObject);
                 }
             }
         }
         `





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 ThatGuyBrian · Oct 17, 2016 at 09:00 PM 0
Share

By the way, the code in the original post was for the bullet, not the shooting script. If you want the shooting script, I have it right here.

  using UnityEngine;
     using System.Collections;
     
     public class shooting : $$anonymous$$onoBehaviour {
     
         public GameObject bulletAmmo;
     
         // Use this for initialization
         void Start () {
         
         }
         
         // Update is called once per frame
         void Update () {
             if (Input.Get$$anonymous$$ouseButtonDown(0))
             {
                 RaycastHit hit;
                 Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50f);
                 GameObject bullet = Instantiate(bulletAmmo, transform.position, Quaternion.identity) as GameObject;
                 bullet.transform.LookAt(hit.point);
             }
         }
     }
     

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by doublemax · Oct 17, 2016 at 09:08 PM

 transform.Translate(Vector3.back * 20 * Time.deltaTime);

Try this instead:

 transform.Translate(transform.forward * 20 * Time.deltaTime);
Comment
Add comment · Show 6 · 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 ThatGuyBrian · Oct 17, 2016 at 09:45 PM 0
Share

I still have the same problem.

avatar image doublemax ThatGuyBrian · Oct 18, 2016 at 05:23 AM 0
Share

The same? Nothing changed?

Can you describe the bullet path? Where does it go and does it change at all when you aim in different directions?

avatar image ThatGuyBrian doublemax · Oct 18, 2016 at 11:10 AM 0
Share

It only goes in one direction. If you're facing south, for example, it'll fire straight, but if you're facing to the right then it'll fire to the left. It's only firing southbound. The direction of the bullet is not relative to the player, but relative to the set direction. I was looking for a solution that would track the player's direction and fire in said direction.

Show more comments
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

89 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How does one continuously move an object forward without using velocity? 1 Answer

How do I define the direction and speed of a projectile separately? (JS) 0 Answers

Bullet clones will not be destroyed. 2 Answers

Instantiate vs Instantiate as gameobject 1 Answer

Aiming and shooting to the same point in third person 1 Answer


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