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 timzydee · Mar 23, 2013 at 03:58 AM · physicsmousesmoothshoot

Need Javascript to propel an object toward mouse click

I've seen a lot of variations on this request but none of them are exactly what I need. I am new to coding so I can't just take snipets here and there. I need a javascript that will handle the following:

A physics object will move toward the mouse click location at a constant rate of speed. After the click, the object will continue on in that direction and ignore any other clicks and just collide and interact with things as the physics dictates. I was using a script that would move the object to the exact position where the mouse clicked but that wasn't right because it just stopped dead at that position instead of continuing to travel on.

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 $$anonymous$$ · Mar 25, 2013 at 09:13 PM

for the traveling on mouse click, you could use something like:

 var hit : RaycastHit;
 var ray = Camera.main.SreenPointToRay(Input.mousePosition);
 
 if(Physics.Raycast(ray, hit, Mathf.Infinity))
 {
    Debug.Log(hit.point);
 }

In that case, hit.point would return where you clicked. To make you object move towards your clicked point, use:

 ObjectToMove.position = Vector.Lerp(ObjectToMove.position, hit.point, Time.deltaTime)
Comment
Add comment · Show 4 · 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 $$anonymous$$ · Mar 25, 2013 at 09:06 PM 0
Share

If you want a complete Script, here you go :)

 var Clicked : boolean = false;
 var ObjectTo$$anonymous$$ove : Transform;
 
 function Update()
 {
    //If the Player hits the left mouse button
    if(Input.Get$$anonymous$$ouseButtonDown(0))
    {
       var hit : RaycastHit;
       var ray = Camera.main.SreenPointToRay(Input.mousePosition);
  
       if(Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity))
       {
          Clicked = true;
          var clickPoint : Vector3 = hit.point; 
       }
    }
 
    if(Clicked)
    {
       //$$anonymous$$oves the Object towards the clicked point
       ObjectTo$$anonymous$$ove.position = Vector.Lerp(ObjectTo$$anonymous$$ove.position, hit.point, Time.deltaTime)
 
       if(Vector3.Distance(ObjectTo$$anonymous$$ove.position, clickPoint) < 0.1)
       {
           Clicked = false;
       }
    }
    else
    {
       //Insert the rest of your script here :)
    }
 }
avatar image timzydee · Mar 26, 2013 at 02:36 AM 0
Share

Thanks for the reply with the code.

However... 1. The object starts to move toward the mouse click but then changes its $$anonymous$$d and goes back to its original facing and heads straight forward from there. 2. After the first click, I'm not able to click a new position again. The object looks like it is trying to move but just sits still. 3. It also doesn't seem to recognize clicks from behind the object, only those out in front. 4. In another test case, just a plane for the ground and a sphere for the object to move, the sphere just sort of sinks down into the plane for a few seconds and comes to a stop.

$$anonymous$$aybe I'm doing something wrong.

avatar image timzydee · Mar 26, 2013 at 02:41 AM 0
Share

By the way, here's the script I've been using. It's perfect except that I would like to use Addforce ins$$anonymous$$d so that it doesn't stop at the target click. I'm new to coding so all of my attempts at altering the code toward that end have just been bad guesses with errors.

 var smooth:int; // Deter$$anonymous$$es how quickly object moves towards position
  
 private var targetPosition:Vector3;
  
 function Update () {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
     {
         var playerPlane = new Plane(Vector3.up, transform.position);
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hitdist = 0.0;
  
         if (playerPlane.Raycast (ray, hitdist)) {
             var targetPoint = ray.GetPoint(hitdist);
             targetPosition = ray.GetPoint(hitdist);
             var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             transform.rotation = targetRotation;
         }
     }
  
     transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
 }
avatar image timzydee · Mar 28, 2013 at 08:20 PM 0
Share

I finally found the answer here

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannonball physics? 1 Answer

Converting button to mouse click 1 Answer

2D 360 degress platformer example needed 0 Answers

Make on object hinged to the player follow the cursor 1 Answer

2d Spaceship smooth mouse movement 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