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 /
  • Help Room /
avatar image
0
Question by Lazybones94 · May 24, 2016 at 07:55 PM · gameobjectdirectionmove

Move GameObject in a direction of a mouse but longer

I have a turret that is firing in the direction of my mouse. My current code is:

 void Fire()
 {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         mousePos.z = 0;
         direction = Vector3.MoveTowards(projectile.transform.position, mousePos, 50);
 }
 
 void Update()
 {
         proj.transform.position = Vector3.MoveTowards(projectile.transform.position, direction, speed * Time.deltaTime);
 }

The projectile moves to where mouse position was once I fired it, but then stops. I need it to go on moving in the same direction even when it reaches the position where mouse was at the moment of fire. In other words I need either to prolong the distance between mouse position and projectile start position or just move it infinitely in the same direction.

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 ricke44654 · May 25, 2016 at 01:42 AM

I think to make this work the way you want, I would just adjust the position of the projectile manually in the Update function. You've got your direction set from the Fire function, so instead of using MoveTowards in Update, you'd do something like this:

             proj.transform.position += (direction * speed * Time.deltaTime);
 

I setup a little test project to test it out myself and duplicated what you were seeing. Then I changed it to the above code and that seemed to do the trick. You'll need some way to dispose / disable the object after some condition is met.

One possibility might be a sphere collider around your turret set to the maximum range of a shot. The collider could be set as a Trigger collider and you could use the OnTriggerExit event to determine when the bullet leaves the collider. Never tried it as I'm still Unity noobish, but seems like that could work.

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 Lazybones94 · May 27, 2016 at 07:10 PM 0
Share

This is behaving wrong when working with negative values. In certain conditions projectiles strike in the opposite direction of where they're supposed to, and sometimes trajectory is very distorted. But thanks for answering.

For now projectiles are destroyed when they are not seen by any camera.

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

63 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

Related Questions

Problem with destroying a gameobject 2 Answers

Locking facing direction or changing it after following the mouse position 0 Answers

Very weird. Transform position is at two places at the same time. 1 Answer

Moving GameObject slowly OnTriggerEnter 1 Answer

Gameobject doesnt move after build 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