- Home /
C# Raycast 2D GameObject follow Mouse
I'm trying to get a Gameobject that is found by my RaycastHit2D to follow my mouse while I hold down the left mouse button. The Raycast is hitting the Gameobject because the debug.log pops up. But for some reason the Gameobject isn't following my mouse. It works fine when directly attaching a script to the Gameobject without using a raycast like the following below.
void LateUpdate ()
{
Vector3 mousePositions = new Vector3(transform.position.x + Input.mousePosition.x/sensitivity,transform.position.y + Input.mousePosition.y/sensitivity,transform.position.z);
if (Input.GetMouseButtonDown (0)){
Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
transform.position = Vector3.Lerp(transform.position, mousePositions, Time.smoothDeltaTime/sensitivity);
}
}
So I'm not entirely sure what's wrong with my current script. It's an exact copy of the above same but with some raycasting modifications. I tried hit.collider.Gameobject instead of just hit but that didn't seem to make a difference.
void LateUpdate () {
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, transform.position);
Debug.DrawLine(Vector2.zero, pos, Color.cyan);
if (hit.collider != null) {
if(Input.GetMouseButtonDown (0)){
Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
Vector3 mousePositions = new Vector3(hit.transform.position.x + Input.mousePosition.x/sensitivity,hit.transform.position.y + Input.mousePosition.y/sensitivity,hit.transform.position.z);
hit.transform.position = Vector3.Lerp(hit.transform.position, pos, Time.smoothDeltaTime/sensitivity);
}
}
}
Answer by HarshadK · Sep 23, 2014 at 05:55 AM
Since you are moving the gameobject using Vector3.Lerp it will actually not be present under your mouse which is what causing the problem.
GameObject gameObjectToMove;
void LateUpdate () {
if(Input.GetMouseButtonDown (0)){
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, transform.position);
Debug.DrawLine(Vector2.zero, pos, Color.cyan);
if (hit.collider != null) {
gameObjectToMove = hit.collider.gameObject;
}
}
if(Input.GetMouseButtonDown (0) && gameObjectToMove != null){
Vector3 mousePositions = new Vector3(gameObjectToMove.transform.position.x + Input.mousePosition.x/sensitivity,gameObjectToMove.transform.position.y + Input.mousePosition.y/sensitivity,gameObjectToMove.transform.position.z);
gameObjectToMove.transform.position = Vector3.Lerp(gameObjectToMove.transform.position, pos, Time.smoothDeltaTime/sensitivity);
}
if(Input.GetMouseButtonUp (0) && gameObjectToMove != null)
{
gameObjectToMove = null;
}
}
I've just modified the logic to include that when you Raycast and there is a hit then set that game object as target gameObjectToMove. Now if mouse is down then we move that gameObjectToMove along with mouse (your code is just modified to include the same) and then when mouse button is up we set gameObjectToMove to null.
I want to make it so after I've released the mouse button the gameobject keeps moving. Any idea how I could do that? I tried giving my gameobject a rigidbody and placing rigidbody.AddForce in the if(Input.Get$$anonymous$$ouseButtonUp (0) if statement. But the gameobject isn't moving after I've let go of the mouse button.
if(Input.Get$$anonymous$$ouseButtonUp (0) && gameObjectTo$$anonymous$$ove != null)
{
gameObjectTo$$anonymous$$ove.rigidbody2D.AddForce(Vector3.Lerp(gameObjectTo$$anonymous$$ove.transform.position, pos, Time.smoothDeltaTime/sensitivity));
gameObjectTo$$anonymous$$ove = null;
}
What is happening according to current snippet of yours is that the Force is applied only one frame where the mouse button is up. And since this force isn't enough to keep game object moving it is not moving much or not moving at all.
Do you want game object to follow your mouse even after the mouse button is up? or do you want the game object to keep moving forward after the mouse button is up until user does not select another game object to move? Or you want game object to move certain distance forward after the mouse button is up?
I want my game object to move a certain distance(The speed that the mouse was going before I let go of the mouse button) forward after the mouse button is up. How would you find the speed that your mouse is moving?
Set a timer variable to zero when your mouse button is down and for all the time your mouse button is pressed down add deltaTime to it. Now when mouse button is up the value in timer is the total time for which the mouse button was down. Similarly when your mouse button is down store its position and when mouse button is up get current position of mouse and subtract it from the start position, this is your distance traveled.
Now you can easily calculate the speed using formula, speed = distance/time.
Then apply a force with Force$$anonymous$$ode.Impulse to your object based on the direction of movement of mouse (direction can be calculated using the start position and last position).
The method used to calculate the distance above will work properly in case of mouse being moved in straight line. For circular movements it may produce unrealistic results.
I tried putting in Forcemode.Impulse right next to my Vector3.Lerp. But I keep getting an error saying I have invalid arguments. I'm fairly certain that's how you implement Forcemode.Impulse. Do rigidbody2Ds have access to Forcemode.Impulse like 3d rigidbodies?
gameObjectTo$$anonymous$$ove.rigidbody2D.AddForce(Vector3.Lerp(gameObjectTo$$anonymous$$ove.transform.position, pos, Time.smoothDeltaTime/sensitivity * timer),Forcemode.Impulse);