Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Forskan · Apr 15, 2014 at 04:30 PM · c#directionmouseposition

Move GameObject In A Direction

Hello! Im a newbie in unity, and have only been programming for like a year and a half so not that experienced.

Anyways.. Im working on a school project with some classmates, its going to be a moba, similiar to lols all random all mid.

So.. I have a character which throws a spikeball when I press a button, and the ball is moving towards the mouse position that was when I pressed the "fire" button.

I simply use Vector3.MoveTowards for this, and pass in the initial mouse position coordinates.

Now the problem that this only makes the ball go to the mouse position, when it has reached the mouse position (or "targetPosition") it just stops and falls down to the plane.

What I want is that my gameobject should move in the direction of the mouse position, and just continue moving in that same direction once it has reached the mouse position.

I hope this makes sence, english isnt my 1st language and if there's something that you dont understand, please tell me and I will try to explain it better.

Now if some awesome person could point me in the direction of some helpfull methods, or write some code for me, that would be extremely helpfull.

EDITED: This is the script that is attached to the "spikeball".

 public float lifetimeSeconds = 2f;
 
 private float _projectileSpeed = 5f;
 
 private Vector3 targetPosition;
 
 void Start () {
 
 targetPosition = GetMousePosition ();     
 }
 
 
 void Update () {
 
     this.gameObject.transform.position =  Vector3.MoveTowards(this.gameObject.transform.position, 
                                                              targetPosition,
                                                              _projectileSpeed * Time.deltaTime);
 
     Destroy(gameObject, lifetimeSeconds);
 }
 
 private Vector3 GetMousePosition(){
 
     Plane playerPlane = new Plane(Vector3.up,
                                   this.gameObject.transform.position); 
 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     float hitdist = 0.0f; 
     Vector3 mousePosition = Vector3.zero;
          
     if (playerPlane.Raycast (ray, out hitdist)) {
     mousePosition = ray.GetPoint (hitdist);
     }
  
     return mousePosition;
 }

Thanks // Forskan.

Comment
Add comment · Show 3
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 robertbu · Apr 15, 2014 at 04:34 PM 0
Share

Please post the code where you calculate the end point and your movement code. This code will help get you the right answer for your situation.

avatar image Forskan · Apr 15, 2014 at 04:53 PM 0
Share

Added it :)

avatar image ZDS Alpha · Apr 15, 2014 at 05:05 PM 0
Share

Save the gameobject rotation.

Set the direction/rotation of object to the direction you want to move the object.

then add this line to move object

gameObject.transform.translate(Vector3.forward * distance);

Set the saved rotation of gameobject to gameobject.

1 Reply

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

Answer by robertbu · Apr 15, 2014 at 05:05 PM

Given that 'targetPosition' is a Vector3 and therefore will not change over time, the easiest solution is a hack...move your target position beyond the target. In the case of your code above, you could do it this way:

 void Start () {
     targetPosition = GetMousePosition ();
     targetPosition = transform.position + (target.Position - transform.position).normalized * 1000.0f;    
 }

Another common solution is to move the object forward. So your code in Start() would be:

 void Start () {
     targetPosition = GetMousePosition ();
     transform.LookAt(targetPosition);
 }

Then line 15 becomes:

 transform.Translate(0.0f, 0.0f, _projectileSpeed * Time.deltaTime);

The third solution is to establish a world direction and move the object in that direction. So Start() becomes:

 void Start () {
     targetDirection = (GetMousePosition() - transform.position).normalzied;    
 }

Then your movement code would be:

 transform.position += targetDirection * _projectileSpeed * Time.deltaTime;

Note I've changed the name of the variable from 'targetPosition' to 'targetDirection'.

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 Forskan · Apr 15, 2014 at 06:38 PM 0
Share

Worked perfectly, I was thinking of doing something like the first example but had no clue on how I would do it. A big thanks for the help!

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to know from which direction there is a collision with an object? in C# 1 Answer

Bullets direction is not good (network) 0 Answers

8 Directional sprite in 3d world, how to retrieve direction? 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