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 MatthewFunctions · Jun 19, 2013 at 04:37 AM · cameraobjectscrolldetect

OnMouseUp() Update? Movement.

Hi, I am trying to figure out whether OnMouseUp() updates per frame or not among a few other things. the reason being, I am trying to get my game object to move from point A to point B when I use OnMouseUp (character is clicked on, then the player drags the mouse to point B, then upon release of the mouse button the character moves from point A to B).

I am able to select the character and detect the mouse dragging. I am having trouble when I release for the character to move to point B and show the object traveling rather than appearing there. I will post my code. Thank you very much.

[CODE]

 #pragma strict
 
 var target : Transform;
 var speed : float;
 var selected = false;
 
 
 function Start () {
 
 }
 
 function Update () {
 
 if (selected){
 
 // The step size is equal to speed times frame time.        
 var step = speed * Time.deltaTime;               
 
  // Move our position a step closer to the target.       
  transform.position = Vector3.MoveTowards(transform.position, target.position, step);
 }
 
 }
 
 function OnMouseUp() {
 
 Debug.Log ("Drag Ended.");
 
 var step = speed * Time.deltaTime;
 transform.position = Vector3.MoveTowards(transform.position, Input.mousePosition, step);
 
 }
 
 function OnMouseDrag(){
 
    var move = transform.TransformDirection(Vector3(0, 0, 77));
 
    renderer.material.color -= Color.blue * Time.deltaTime;
     
     Debug.DrawRay(transform.position, move, Color.red);
     
 if (Physics.Raycast(transform.position, move, 77)){
 Debug.Log("Hi.");
 }
 }

[/CODE]

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 MatthewFunctions · Jun 25, 2013 at 05:31 AM 0
Share

When I click and drag it to the point I want the object to move, it does not read the click location it seems. What am I doing wrong?

avatar image fafase · Jun 25, 2013 at 09:21 AM 0
Share

Do you want the move after releasing the button?

avatar image MatthewFunctions · Jun 25, 2013 at 07:59 PM 0
Share

Yes. When I release the mouse button, I would like for the character to move from point A to B.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bubzy · Jun 25, 2013 at 05:56 AM

looks like you aren't giving the raycast the opportunity to go where you click, you are clamping it to a certain set of variables.

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 if (Physics.Raycast (ray, 100)) {
     print ("Hit something");
 }

from http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

you could also try moving the debug to OUTSIDE of the if{} statement, this would let you know if the drag is working all the time, and not based on the condition you have set

Comment
Add comment · Show 3 · 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 MatthewFunctions · Jun 25, 2013 at 06:15 AM 0
Share

Thank you for your response. Okay, the ray is hitting the floor since I added the debug outside of the if{} statement like you suggested. Now my drag and my ray are both working accurately. Now, if you do not $$anonymous$$d, how do I get my character to move to that point B now that they are detected? Thank you again, I have had very much trouble with this for awhile.

avatar image bubzy · Jun 25, 2013 at 09:16 AM 0
Share

try using the LookAt function and then moving the character in the vector3.forward direction

avatar image MatthewFunctions · Jun 25, 2013 at 08:28 PM 0
Share

Vector3.forward will not let me move it toward the mouse position when mouse click is released. Not that I know of. I try and do it with this code:

 [CODE]
 
 function On$$anonymous$$ouseUp() {
 
 Debug.Log ("Drag Ended.");
 
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 
 if (Physics.Raycast (ray, hit, 100)) 
 {    
 
 Debug.DrawLine (ray.origin, hit.point);
 
 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Input.mousePosition, speed * Time.deltaTime);
 
 }
 print ("Hit something");
 }
 
 [/CODE]

It seems that when I release On$$anonymous$$ouseUp(), it still does not move torward my click position when I release. Could it be my "transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Input.mousePosition, speed * Time.deltaTime)"? I read the Script Reference for Vector3.$$anonymous$$oveTowards and I am still not sure what to put in the brackets. It seems right that Input.mousePosition is in there. Thank you.

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

15 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

Related Questions

Activate object if visible and Deactivate object if invisible 2 Answers

Calculate distance between AR object and camera 1 Answer

Render tooltip on top of everything with MRTK 0 Answers

Make RenderTexture turn certain color to Transparent? 2 Answers

How to make a camera follow an object 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