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 asmith29 · Apr 17, 2018 at 01:59 AM · movementprogrammingprojectile

Problem with bullet moving in straight line

I thought this problem would be straightforward but I am running into a strange issue.

I am making a tower defense game and so I have towers shooting bullets at incoming enemies. When an enemy enters the towers "radius of detection" I find the direction the bullet needs to travel by subtracting the enemies position from the tower's position and normalize that vector. I then create a bullet prefab and pass that direction in. To move the bullet, I have the following code in the update function:

 transform.position += direction * speed * Time.deltaTime;`

But when the game runs, it seems that the movement has been decomposed into the corresponding axes. For example if the resulting vector of direction * speed * Time.deltaTime were (.5, -.8, 0), it would first move horizontally right .5, then move down vertically .8. It resembles a stair shape opposed to a straight line. I have no idea why, is there something I am misunderstanding?

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 MarioSantoso · Apr 17, 2018 at 03:31 AM 0
Share

Post your code so we can help you better

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jon_Brant · Apr 17, 2018 at 02:06 AM

Do you have your code that moves the bullet in Update()? Try moving it to FixedUpdate()

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 asmith29 · Apr 17, 2018 at 02:35 AM 0
Share

FixedUpdate() results in the same problem.

avatar image
0

Answer by MarioSantoso · Apr 17, 2018 at 02:12 AM

Try doing this (pseudo code)

  1. Enemy enter tower's detection radius.

  2. Get the enemy transform.

  3. Instantiate bullet prefab.

  4. Use LookAt function to make the bullet look at the enemy.

  5. Use "transform.Translate(Vector3.forward moveSpeed Time.deltaTime, Space.Self);" to move the bullet toward the enemy.

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 asmith29 · Apr 17, 2018 at 02:34 AM 0
Share

That did not work unfortunately.

avatar image
0

Answer by shadowpuppet · Apr 17, 2018 at 07:04 PM

LookAt as in #4 in previous comment.If bullet has a rigidbody addFOrce to move it in direction of lookAt

 GameObject Bullet = Instantiate (bullet, transform.position , transform.rotation)as GameObject; 
             Bullet.GetComponent<Rigidbody> ().AddForce (transform.forward * throwDistance);
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

126 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

Related Questions

Multiple Cars not working 1 Answer

AddForce/ForceMode.Impulse 1 Answer

shoot forward and comeback 0 Answers

How to get character movement done with box collider 2d? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 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