Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by ShayDekel · May 21, 2019 at 04:07 PM · 2dtopdown2d-gameplayforwardmovinggameobject

Moving A 2D Object Forward

I'm instantiating a bullet out of a gun and trying to move it forward in the direction/rotation is was instantiated as in a 2D space, but for some reason nothing works, the bullet doesn't move or it's moving in the wrong direction. I have tried using transform.forward and transform.right but non of them works. I will be extremely grateful for every help, this is my first time working with Unity and I'm still trying to learn the ropes.

BTW: This is a top down shooter kind of game.

This is how I instantiate the bullet:

 Instantiate(prefab, firingPoint.position, firingPoint.rotation);


And this is the bullet class:

 public class Bullet : MonoBehaviour
 {
     private Vector3 firingPoint;
 
     [SerializeField]
     float bulletSpeed = 2;
 
     [SerializeField]
     private float maxBulletDistance = 20;
 
     // Start is called before the first frame update
     void Start() {
 
         firingPoint = transform.position;
     }
 
     // Update is called once per frame
     void FixedUpdate() {
         MoveBullet();
 
         if (Vector3.Distance(firingPoint, transform.position) > maxBulletDistance) {
             Destroy(this.gameObject);
         }
     }
 
     void MoveBullet() {
 
         transform.position += transform.right * bulletSpeed * Time.deltaTime;
     }
 }


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
1

Answer by xxmariofer · May 21, 2019 at 04:12 PM

first, change the fixedupdate to update (wont fix your issue but fixedupdate should only be used for physics.) there are only 2 posibilities you are using the wrong vector(try using transform.right/left/forward) and if it doesnt work it means you have set somewhere the time.timescale to 0, (so time.deltatime is 0 to)

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 ShayDekel · May 21, 2019 at 04:44 PM 0
Share

First of all, thank you for the quick replay. I tried using what you said, but transform.left doesn't exist in my code and the other 2 options move the bullet in random directions. Also, I never touched the timescale so I don't believe this to be the issue...

avatar image xxmariofer ShayDekel · May 21, 2019 at 05:51 PM 0
Share

If they are moving but in random directions it is because the object is rotated,if the object is rotated there qre 2 options, the first just change transform.right for Vector3.right (also test Vectoe3.forward since depends of how you set up your world) or a second solution is create an empty object with 0,0,0 posituin and rotation, make the player child of the new object and make only attach that script TO THE PARENT not the child

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

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

OnTriggerEnter2D Running twice when destroying game object. 1 Answer

Instantiate after setting values? 0 Answers

Instantiated objects do not work with Hitboxes(2D) 0 Answers

Draw Gizmos for Non-Monobehaviour objects in an Array 0 Answers

Add acceleration and deceleration for top down 2D movement. 2 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