- Home /
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]
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?
Yes. When I release the mouse button, I would like for the character to move from point A to B.
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
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.
try using the LookAt function and then moving the character in the vector3.forward direction
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
![](https://koobas.hobune.stream/wayback/20220613104439im_/https://answers.unity.com/themes/thub/images/avi.jpg)