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 /
avatar image
0
Question by Mattnet · Jun 25, 2017 at 05:32 PM · rotationmovement scriptdirectionbullet

Can't make a bullet move in the direction the actor is facing

Hey people, as the title says, I'm having problems moving bullets in the right way. I have an actor with fixed position but variable rotation. When it fires, the bullets always move in a straight line upwards instead of moving in the direction that the actor is facing. The bullet changes its rotation with the actor but it still moves upwards.

Here is the WeaponScript attached to the actor. It instantiates the bullet, setting the starting position and the bullet rotation.

 public class WeaponScript : MonoBehaviour {
 
     private float fireCooldown;
 
     // Use this for initialization
     void Start () {
         fireCooldown = 0f;
     }
     
     // Update is called once per frame
     void Update () {
         if (fireCooldown > 0f) { fireCooldown = fireCooldown - Time.deltaTime; }
     }
 
     public void Fire () {
         if (fireCooldown<=0f)
         {
             fireCooldown = WeaponStatsScript.instance.fireRate;
             BulletScript bullet;
             var shotObject = Instantiate(Resources.Load(WeaponStatsScript.instance.bulletPrefab)) as GameObject;
             shotObject.transform.position = transform.position;
             shotObject.transform.rotation = transform.rotation;
         }
     }
 }

And here is the BulletScript. Speed is (10,10).

 public class BulletScript : MonoBehaviour {
 
     private int damage;
     private Vector2 speed;
     private Vector2 direction;
     private Vector2 movement;
     private Rigidbody2D rigidbodyComponent;
 
     // Use this for initialization
     void Start () {
         Destroy(gameObject, 10);
         damage = WeaponStatsScript.instance.bulletDamage;
         speed = WeaponStatsScript.instance.bulletSpeed;
         direction = new Vector2(0, 1);
         rigidbodyComponent = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update () {
         movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
     }
 
     private void FixedUpdate()
     {
         if(rigidbodyComponent==null)
         {
             Debug.LogError("00003 - BulletScript - rigidbodyComponent not initialised!");
             rigidbodyComponent = GetComponent<Rigidbody2D>();
         }
         rigidbodyComponent.velocity = movement;
     }
 }
 

The weird thing is that a very similar code worked in another project, I'm unsure why it does not behave correctly anymore. Anyone can help? Thanks a lot in advance!

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 Mattnet · Jun 27, 2017 at 08:08 AM 0
Share

Anyone else that can help and suggest possible causes and solutions? As suggested by other I tried adding shotObject.transform.rotation = transform.rotation; in the WeaponScript after instantiating the bullet and changing the movement in the BulletScript to movement = new Vector2(speed.x * direction.x, speed.y * direction.y); but as a result the bullet spawns and doesn't move at all. Creating a public static PlayerScript playerObject; in the PlayerScript, then doing direction = PlayerScript.playerObject.transform.eulerAngles also had the same result.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cbjunior · Jun 25, 2017 at 06:08 PM

Because you have the direction = new Vector2 (0, 1) line, you are completely nullifing the horizontal movement value and keeping the vertical movement value, thus your bullets are only flying upwards. Try changing the line to use the actual rotation of the GameObject as opposed to a static value.

Comment
Add comment · Show 1 · 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 Mattnet · Jun 25, 2017 at 08:59 PM 0
Share

I tried adding shotObject.transform.rotation = transform.rotation; in the WeaponScript after instantiating the bullet and changing the movement in the BulletScript to movement = new Vector2(speed.x * direction.x, speed.y * direction.y); but as a result the bullet spawns and doesn't move at all.

avatar image
0

Answer by Litleck · Jun 25, 2017 at 06:43 PM

Add a new Public GameObject Character; and set it to your character then use this which will make the bullet go in the direction of the Character. direction = Character.transform.eularAngles That should work.

Comment
Add comment · Show 2 · 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 Mattnet · Jun 25, 2017 at 09:05 PM 0
Share

I did what you said and created a public static PlayerScript playerObject; in the PlayerScript, then did direction = PlayerScript.playerObject.transform.eulerAngles; as you said, keeping movement the same as posted. The result is that bullets spawn with the right rotation (which I had before as well) but now they don't move at all.

avatar image Litleck Mattnet · Jun 29, 2017 at 03:23 AM 0
Share

Use this at the very start of the bullet life then have a function to move it forward and thats it. Also ins$$anonymous$$d of using a constant Update function you should make a custom function that runs when the bullet is shot and ends when the bullet collides with something.

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

86 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

Related Questions

making bullet go travel at angle fired and limit gun rotation in Unity C# version 5.2 2D 1 Answer

Spawn bullet in front of object then apply force? 1 Answer

Shoot an object and have it move based on rotation 1 Answer

Player rotating towards direction of movement in 3D 1 Answer

Problem with Shooting Accuracy 0 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