- Home /
The question is answered, right answer was accepted
Why Line Renderer always move towards the same spot in perspective projection but not in orthographic projection.
When i am using perspective position of camera then no matter where i click on the screen, the ray is always goes towards a fixed point, which is (0,0,0) i think. But it is not the matter with orthographic projection. It works fine, the rays always move towards the projected position(using mouse click) in orthographic projection.
This is my script:
public class onClickShoot : MonoBehaviour { LineRenderer line; public Transform initialState; Vector3 targetState; private WaitForSeconds shotDuration = new WaitForSeconds(.5f);
void Start()
{
line = GetComponent<LineRenderer>();
}
void Update()
{
line.SetPosition(0, initialState.position);
if (Input.GetMouseButtonDown(0))
{
getTarget();
StartCoroutine(shooting());
}
}
void getTarget() {
targetState = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetState.z = 10;
line.SetPosition(1, targetState);
}
private IEnumerator shooting()
{
line.enabled = true;
yield return shotDuration;
line.enabled = false;
}
}
Answer by nikhilVardhan · Jun 28, 2020 at 06:06 AM
I used this script to solve the error.
Script:
public class getObjectPosition : MonoBehaviour { LineRenderer line; private void Start() { line = GetComponent(); } private void Update() { line.SetPosition(0, transform.position); if (Input.GetMouseButtonDown(0)) { float rayLength = 100.0f; // This is the length of the ray. You need to figure out how long it must be in order to hit all of the cubes in the scene. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayLength))
{
if (hit.collider != null)
{
Debug.Log(hit.collider.transform.position);
line.SetPosition(1,hit.collider.transform.position);
}
}
}
}
}