Search Unity

shoot with a Raycast

Discussion in 'Physics' started by truthseeker089, Jun 23, 2019.

  1. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    hi guys I need help in improving my RAYCAST I used this in turret but now I want to assign it to a soldier can anyone please help? thanks!


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class raycasting : MonoBehaviour {
    public float rotationSpeed;
    public float distance;
    public GameObject bullet;
    void Start () {
    Physics2D.queriesStartInColliders = false;
    }

    void Update () {
    transform.Rotate (Vector3.forward * rotationSpeed * Time.deltaTime);
    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, distance);
    if (hitInfo.collider != null) {
    Debug.DrawLine (transform.position, hitInfo.point);
    if (hitInfo.collider.CompareTag ("friend")) {
    Instantiate (bullet, gameObject.transform.position, gameObject.transform.rotation);
    }
    } else {
    Debug.DrawLine (transform.position, transform.position + transform.right * distance);

    }
    }
    }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What do you need help with, exactly?
    and how is this related to the physics forum? I believe this belongs to scripting.