Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NicoGm · Feb 27, 2021 at 08:43 AM · movement scriptmove an objectunexpected

My bullet keeps following its spawn point after being instantiate. Pls help

Im making a simple top down shooter, but

when I fire and rotate the player, my bullet follows my player rotation.

alt text

I think it looks cool but I want it to go in a straigh line. I have no idea how to fix this. Please help.

Here's my bullet script

 public GameObject go;
     private Rigidbody2D bRB;
 
     public float bulletLifespan;
     public float bulletLifespanReset;
     public float bulletSpeed;
 
 
     void Start()
     {
         bRB = GetComponent<Rigidbody2D>();
         go = GameObject.Find("BulletSpawner");
 
         
     }
     // Update is called once per frame
     void Update()
     {
         BulletMvm();
         Despawn();
     }
 
     void BulletMvm()
     {
 
         Transform goTransform = go.GetComponent<Transform>();
         transform.Translate(goTransform.transform.right * bulletSpeed * Time.deltaTime);
         
     }

Here's where i spawn my bullet

 public class BulletSpawner : MonoBehaviour
 {
     public GameObject bulletPrefab;
     public Transform spawnPoint;
     //public NormalBullet s_NB;
 
     public float timer;
     public float fireRate;
 
     // Update is called once per frame
     void Update()
     {
         ActiveShoot();
     }
 
     public void ActiveShoot()
     {
         if (timer <= 0.0f)
         {
             Shoot();
             timer = fireRate;
         }
         timer -= Time.deltaTime;
     }
 
     public void Shoot()
     {
         
 
         float x = spawnPoint.position.x;
         float y = spawnPoint.position.y;
 
         Vector2 sP = new Vector2(x, y);
         
         ObjectPooler.Spawn(bulletPrefab, sP, Quaternion.identity);
     }
 }


topdownproject-test-pc-mac-linux-standalone-unity.gif (319.0 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Waterpolo7 · Dec 22, 2021 at 08:14 PM

Hi @NicoGm I think I know how to solve this. Currently the bullet will always go in the direction of the players 'right', even after the shot is launched. To not have this problem, add this line to your Start void:

 Transform goTransform = go.GetComponent<Transform>();
 Vector3 directionOfBullet = go.right;

This will make sure your direction doesn't change anymore. In the Update void you should now delete the:

 Transform goTransform = go.GetComponent<Transform>();

and you change the transform.Translate to:

 transform.Translate(directionOfBullet * bulletSpeed * Time.deltaTime);

*the whole bullet script so you can copy, the above part was for explaining:

  public GameObject go;
      private Rigidbody2D bRB;
  
      public float bulletLifespan;
      public float bulletLifespanReset;
      public float bulletSpeed;
  
  
      void Start()
      {
          bRB = GetComponent<Rigidbody2D>();
          go = GameObject.Find("BulletSpawner");
          //This is to make clear what direction he must go in: V
          Transform goTransform = go.GetComponent<Transform>();
          Vector3 directionOfBullet = go.right;
          
      }
      // Update is called once per frame
      void Update()
      {
          BulletMvm();
          Despawn();
      }
  
      void BulletMvm()
      {
          //This does the movement, but with the good direction.
          transform.Translate(directionOfBullet * bulletSpeed * Time.deltaTime);
          
      }

I hope this solves your problem. Notify me when it works :)

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

160 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

Related Questions

Prefab 2d interaction with 3d prefab 0 Answers

How do I get a character to walk on walls and ceilings? 1 Answer

Move a selected object. 0 Answers

How to make the player move faster after I press and hold the button? Because now it's only making something like a short jump,How to make player move faster after press and hold the button? 0 Answers

Teleporting 2D object once 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