- Home /
Raycast from Character to Mouse Position?
I am making a 2D procedurally generated game similar to Terraria or Starbound. However, I use 3D colliders since I am using the Character Controller component on my player. So when my character clicks, I want to break a block and attempt to raycast from the character to wherever the mouse was clicked. This isn't working for some reason so I switched to Debug.DrawRay and I see now the ray is being draw upwards, even though I might have clicked to the left, right, or below the player. I cannot seem to get the line to aim down. What am I doing wrong here?
Vector3 mousePosition = playerCamera.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = 0;
// Draw a debug line (transform.position = character position)
Debug.DrawRay(transform.position, mousePosition, Color.red, 0f);
Also here is a picture of the issue:
You are using ScreenToWorld with just x,y coordinates from mouse screen position so that assumes that you want the Z to be 0. Which is not exactly the position to which you are trying to draw your ray to. There are infinitely many positions that resemble your mouse click position. It might be the position right behind your camera, or the position far far away from it. All those points are a part of one Ray. You can get that ray by using
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
and then pass that to Physics.Raycast to see where that ray hits some object:
RaycastHit hit;
if (Physics.Raycast (ray, hit, 100.0)) {}
Then you can use that 'hit' variable to get the reference to that object that was hit by ray and Destroy it
Answer by robertbu · Mar 06, 2014 at 09:17 PM
You are using DrawRay() with two positions. DrawRay() takes a position and a direction. You can either switch and Debug.DrawLine() (which takes two positions), or you can modify your DrawRay() as:
Debug.DrawRay(transform.position, mousePosition - transform.position);
Also note that your use of ScreenToWorldPoint() used this way will only work if you have an Orthographic camera. It will not work for a Perspective camera.
Answer by jotapeh_ · Mar 07, 2014 at 12:07 AM
As has already been said, you need to create a ray from the Camera outwards where the mouse is clicked..
Ray mseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
However, your question asks for a Raycast from the player to the point where the mouse was clicked. Probably then you want the player's plane:
Plane playerPlane = new Plane(Vector3.back, player.transform.position);
float d;
if (playerPlane.Raycast(mseRay, out d)) {
Vector3 hitPt = mseRay.GetPoint(d);
// click would hit the same vertical plane as the player at hitPt
}
Then within that block you would create a Ray from the player's position to hitPt, like this:
Ray playerToClick = new Ray(player.transform.position, hitPt - player.transform.position);
Raycast as normal from there.
@jotapeh_ - your code works well in a 3D environment incliding a Perspective camera where the camera is at an angle relative to the axes. The OPs code will only work in the more restricted situation of a axes aligned, Orthographic camera. But it will work given the restrictions.
Your answer
Follow this Question
Related Questions
2D Character Flip 2 Answers
Camera.ScreenToWorldPoint with Perspective camera 1 Answer
How to create a multilayered 2D effect? 1 Answer
Unity Android Movement Problem 0 Answers
Ray camera to Terrain goes Wrong. 1 Answer