• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Gameobject doesnt move after build 0 Answers

How to move this Gameobject to another Gameobject by Speed? 1 Answer

GameObject doesn't move 1 Answer

move one object to defined point according to another object movement to that point 0 Answers

Problem with destroying a gameobject 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges