Question by 
               jamesorion44 · Oct 28, 2020 at 08:45 PM · 
                scripting problemraycast3dcargun script  
              
 
              Gun won't face target
I'm making a game where the player is driving a car with a mounted gun. In order to shoot enemy you just need click the shoot button and the gun will automatically shoot nearby enemies. But for some reason, the gun looks around at things that aren't enemies instead of the actual enemies even though I told the gun what to aim at with this script. Did I do something wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gun1 : MonoBehaviour {
 public float lookRadius = 10.0f;
 public Transform target;
 public GameObject gun;
 public ParticleSystem muzzleFlash;
 public GameObject projectile;
 void Update()
 {
     float distance = Vector3.Distance(target.position, transform.position);
         Debug.DrawRay(transform.position, transform.forward, Color.green);
         Vector3 direction = (target.position - transform.position).normalized;
         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 10f);
     
 }
 void FixedUpdate()
 {
     if (Input.GetButtonDown("Jump"))
     {
         Shoot();
         Instantiate(projectile, gun.transform.position, Quaternion.identity);
     }
 }
 void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.blue;
     Gizmos.DrawWireSphere(transform.position, lookRadius);
 }
 void Shoot()
 {
     Instantiate(muzzleFlash, gun.transform.position, gun.transform.rotation);
     RaycastHit hit;
     Debug.DrawRay(transform.position, transform.forward, Color.red);
     if (Physics.Raycast(gun.transform.position, gun.transform.forward, out hit))
     {
         Debug.Log(hit.transform.name);
     }
 }
} 
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Raycast not taking full length 0 Answers
Jumping with slight air control while retaining velocity 1 Answer
Scripting Errors 1 Answer
Raycast not detected on instantiated object 0 Answers
How to pass event info to events? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                