- Home /
Question by
Moritz2003 · Jul 17, 2020 at 03:19 PM ·
raycasttopdown
Raycast going through objects
So i am programming a little top Down shooter and for that i want to shoot to the position where my mouse is the code: public class TopDownShoot : MonoBehaviour { public BulletController bullet; public float bulletSpeed; public float timeBetweenShots; private float shotCounter; public Transform firePoint;
[SerializeField] private Camera mainCamera;
private void Update()
{
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if(groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
if (Input.GetMouseButton(0))
{
shotCounter -= Time.deltaTime;
if(shotCounter <= 0)
{
shotCounter = timeBetweenShots;
BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController;
newBullet.speed = bulletSpeed;
}
}
}
}
problem is that the raycast goes through the ground idk how to fix it
Comment
Your answer
Follow this Question
Related Questions
Unity 2D Top-Down Raycast length/distance and direction problem 0 Answers
No way to use Raycast infront of player according to rotation in 2D? 1 Answer
making a hole through walls to see my player object in a top down game enviroment 1 Answer
2D Field of View 2 Answers
RayCasting shows wrong point 1 Answer