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 /
avatar image
0
Question by unity_Q1usDZf64kxuhQ · Jun 01, 2021 at 07:41 AM · rotationdirectionprojectile

How to get bullet to face the direction it's going?

So i copied a script from a yt channel named Alexander Zotov. He creates a script that shoots bullets from a bullet pool however none of the bullets face the direction their going hence this result: alt text

What do i need to do in order to get bullets to face the direction they're going?

This is the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletFiring : MonoBehaviour
 {
     [SerializeField]
     private int bulletsAmount = 10;
 
     [SerializeField]
     private float startAngle = 90f, endAngle = 270f;
 
     private Vector2 bulletMoveDirection;
 
     // Start is called before the first frame update
     void Start()
     {
         InvokeRepeating("Fire", 0f, 2f);   
     }
 
     // Update is called once per frame
     private void Fire()
     {
         float angleStep = (endAngle - startAngle) / bulletsAmount;
         float angle = startAngle;
 
         for (int i = 0; i < bulletsAmount + 1; i++) 
         {
             float bulDirX = transform.position.x + Mathf.Sin((angle * Mathf.PI) / 180f);
             float bulDirY = transform.position.y + Mathf.Cos((angle * Mathf.PI) / 180f);
 
             Vector3 bulMoveVector = new Vector3(bulDirX, bulDirY, 0f);
             Vector2 bulDir = (bulMoveVector - transform.position).normalized;
 
             GameObject bul = BulletPool.bulletPoolInstanse.GetBullet();
             bul.transform.position = transform.position;
             bul.transform.rotation = transform.rotation;
             bul.SetActive(true);
             bul.GetComponent<Bullet>().SetMoveDirection(bulDir);
 
             angle += angleStep;
         }
     }
 }
 


screenshot-9.png (5.7 kB)
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Jun 01, 2021 at 02:43 PM

It's easier to make the bullet move in the direction it's facing, by moving it along transform.forward, then you just rotate in the direction you want it to go and AddForce.

Comment
Add comment · Show 3 · 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 logicandchaos · Jun 01, 2021 at 02:44 PM 0
Share

you could get the direction of travel convert it to a rotational value and apply it to the bullet, but that way is more processor intensive and harder.

avatar image unity_Q1usDZf64kxuhQ · Jun 02, 2021 at 10:58 AM 0
Share

yeah, i have no clue how to do that, i was expecting that i only needed to add or change a line of code that'd give me the result i want.

avatar image Llama_w_2Ls unity_Q1usDZf64kxuhQ · Jun 02, 2021 at 01:00 PM 0
Share

You can use a single line of code, which is bullet.rotation = Quaternion.LookRotation(travelDirection);. This returns a rotation that faces a direction. See Here: Quaternion.LookRotation

avatar image
0

Answer by Statement · Jun 02, 2021 at 11:25 AM

Try this, this sets the rotation based on euler angles.

 ...
 bul.transform.position = transform.position;  
 bul.transform.rotation = Quaternion.Euler(0f, 0f, angle); // Changed this line of code!
 bul.SetActive(true);
 ...

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
0

Answer by unity_Q1usDZf64kxuhQ · Jun 02, 2021 at 11:40 PM

alt text

yeah i replaced the bit of code with the one you gave me but it seems that the fireballs rotate on each other and only shoot upwards. I think i missed out on a detail is that the sprite shooting the fireballs does not rotate but idk.


screenshot-10.png (10.5 kB)
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

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

161 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 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

Shoot projectile according to rotation 2 Answers

Convert Object Rotation to X, Y values 1 Answer

Camera/Direction Rotation 2 Answers

Shuriken particle trail rotating 1 Answer

Objects Instantiating at wrong position 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