- Home /
How to make constant click position ?
I've been working on click to go code;
function Update () {
if (Input.GetButton("Fire1")) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
Debug.Log(hit.point);
Debug.DrawRay(ray.origin, hit.point);
}
}
}
I've tried this but it's not getting info which i wanted. I want to get click position of wall front of me
There is a wall named "ClickController" and it has box collider. I want to get it's clicked position when i clicked it.
If you're drawing a ray along the line from the camera to the click position, I'm guessing you wont see it because it's completely perpendicular.
Try drawing a cross at the hit position using Debug.DrawLine. You may need to set the duration of the line to see it for more than a frame. Furthermore, if you're looking in the game view, you might need to enable gizmos to see the line.
Well i've figured out how to do that;
var Speed : float;
function Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if(hit.collider.gameObject.name == "Collisioner") {
transform.position = Vector3.Lerp (transform.position, hit.point, Time.deltaTime * Speed);
Debug.Log(hit.point);
}
}
}
}
I tried to get hit position and it just worked :D
Your answer
Follow this Question
Related Questions
what is the best way to detect if an object has been clicked on? 1 Answer
Colliding Objects 1 Answer
How to toggle off / on raycast on a Gameobject 1 Answer
Collider ignore raycast 1 Answer