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
1
Question by The-BOOM · Mar 13, 2010 at 11:18 PM · movementbulletpath

Getting my bullet to move in a straight line

This is my bullet code:

function Awake () { target = GameObject.FindWithTag("Enemy"); xPosition = target.GetComponent("Transform").transform.position.x; zPosition = target.GetComponent("Transform").transform.position.z; xDifference = Mathf.Abs(transform.position.x - xPosition); zDifference = Mathf.Abs(transform.position.z - zPosition); }

function Update () {
Go (); }

function Go () { if (xDifference < .01) x = 0; else { if (transform.position.x < xPosition) { x = speed Time.deltaTime; } if (transform.position.x > xPosition) { x = -1 speed * Time.deltaTime; } }

if (zDifference < .01) z = 0; else { if (transform.position.z < zPosition) { z = speed * Time.deltaTime; } if (transform.position.z > zPosition) { z = -1 * speed * Time.deltaTime; } }

transform.Translate(x,0,z);

return void;

}

(well, there's more to it, but that's the condensed version) I want the bullet I fire to go in a straight line, but if the object in question isn't an equal distance away in x and z, the bullet unrealistically turns in mid air. Can I please have help?

Comment
Add comment · Show 2
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 Sebas · Mar 14, 2010 at 12:25 AM 0
Share

just out of curiosity and to confirm before posting some answer: is there a reason why you don't simply apply a force to your bullet object? If the force is large enough, the path will be very close to a straight line.

avatar image The-BOOM · Mar 14, 2010 at 09:22 PM 0
Share

no reason actually, and with no gravity set it could be a straight line, thnx for the suggestion

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jason_DB · Mar 14, 2010 at 01:37 AM

The problem is that you only have one speed for both x and z. What is happening is that it goes at a 45 degree angle until either xdifference or zdifference is zero, and then goes straight to the object. Trajectories

What you need to do is make two speeds (one for x and one for z) and them calculate them based on the angle between where the bullet starts and its target, possibly by using the vector between the two points.

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
4

Answer by ESAR · Jul 21, 2010 at 05:00 PM

Or you can do this vector math:

transform.position = transform.position + fSpeed * transform.forward;

and tada the bullet will go straight forward from where you shoot it.

also you can change transform.forward for any direction vector which can be something like this:

vDirection = target.position - transform.position; vDirection = vDirection.normalized;

hope its usefull.

:P

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 The-BOOM · Jul 21, 2010 at 06:36 PM 0
Share

thanks (moar letters)

avatar image Tulsisvt · Jul 22, 2016 at 11:38 PM 0
Share

I had been using translate function with normalized direction vector but it never went in a straight line. This worked perfectly. Thanks!

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

1 Person is following this question.

avatar image

Related Questions

How can I get an object to intercept a moving object's path? 3 Answers

IA vehicle: trajectory according to path of MAYA + index 0 Answers

Bounce into the air 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Rigidbody speed in some senes different?! 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