- Home /
Question by
Rodrigo188 · Apr 19, 2019 at 02:49 AM ·
raycastfirst person shooter
Make prefab go to mouse position with ray cast whe click (shooter like)
Hi, the prefab already spawn to where Im facing it and move forward (to raycast), but now it only instantiate when im clicking with the ray cast on a game object, how coul I shoot to the for e.g? Hope somebody can help. Thanks!
public class Cast : MonoBehaviour {
public GameObject bala;
public GameObject player;
public GameObject spawnPoint;
public Transform playerPos;
// Update is called once per frame
void Update () {
RaycastHit hit;
GameObject projectile;
myRay = mainCamera.ScreenPointToRay(new Vector3(x, y, 0));
//new Ray(transform.position, Vector3.forward);
if (Input.GetButtonDown("Fire1"))
{
if (Physics.Raycast(myRay, out hit, 100))
{
Debug.DrawLine(playerPos.position, hit.point);
projectile = Instantiate(bala, spawnPoint.transform.position, Quaternion.identity);
projectile.transform.LookAt(hit.point);
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectile.transform.forward * 10;
}
//Instantiate(bala, hit.point, Quaternion.identity);
//Instantiate(bala,new Vector3 (player.transform.position.x + 0.5f, player.transform.position.y + 0.25f, player.transform.position.z + 0.7f), Quaternion.identity);
}
}
Comment