In the Skeet Shooting Unity Project , My I have an issue where my raycasting for my pistol is properly coded from the lecture but my bullet hole doesn't instantiate and it doesn't affect the GameObject I shoot at, This is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponScript : MonoBehaviour
{
[Header ("Weapons")]
public GameObject pivotPistol;
public GameObject pivotRPG;
public GameObject pivotPSM;
public string activeWeapon;
[Header ("Raycast")]
public GameObject bulletHole;
public GameObject playerCam;
[Header("RPG")]
public Rigidbody rocket;
public GameObject dummyRocket;
public Transform rocketLP;
void Start()
{
SwapWeapons(pivotPistol);
}
private void Update()
{
#region WeaponSwapping
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SwapWeapons(pivotPistol);
activeWeapon = "Pistol";
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
SwapWeapons(pivotRPG);
activeWeapon = "RPG";
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
SwapWeapons(pivotPSM);
activeWeapon = "PSM";
}
#endregion
if(Input.GetButtonDown("Fire1"))
{
#region Raycasting
if (activeWeapon == "Pistol")
{
RaycastHit hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.TransformDirection(Vector3.for ward), out hit))
{
Debug.DrawRay(playerCam.transform.position, playerCam.transform.TransformDirection(Vector3.for ward) * hit.distance, Color.yellow);
if(hit.collider.gameObject.tag == "Pigeon")
{
hit.collider.gameObject.GetComponent<PigeonScript> ().DoDamage(10);
}
else
{
GameObject clonedBulletHole;
clonedBulletHole = Instantiate(bulletHole, hit.point, Quaternion.LookRotation(hit.normal));
clonedBulletHole.transform.parent = hit.collider.gameObject.transform;
}
}
}
#endregion
#region RPG
if (activeWeapon == "RPG")
{
dummyRocket.SetActive(false);
Rigidbody clonedRocket;
clonedRocket = Instantiate(rocket, rocketLP.position, rocketLP.rotation);
clonedRocket.velocity = rocketLP.TransformDirection(Vector3.forward * 10f);
StartCoroutine(ReloadRPG());
}
#endregion
if (activeWeapon == "PSM")
{
//PSM
}
}
}
void SwapWeapons(GameObject gunToShow)
{
iTween.RotateTo(pivotPistol, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(pivotRPG, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(pivotPSM, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(gunToShow, iTween.Hash(
"x", 0f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
}
IEnumerator ReloadRPG()
{
yield return new WaitForSeconds(1f);
dummyRocket.SetActive(true);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponScript : MonoBehaviour
{
[Header ("Weapons")]
public GameObject pivotPistol;
public GameObject pivotRPG;
public GameObject pivotPSM;
public string activeWeapon;
[Header ("Raycast")]
public GameObject bulletHole;
public GameObject playerCam;
[Header("RPG")]
public Rigidbody rocket;
public GameObject dummyRocket;
public Transform rocketLP;
void Start()
{
SwapWeapons(pivotPistol);
}
private void Update()
{
#region WeaponSwapping
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SwapWeapons(pivotPistol);
activeWeapon = "Pistol";
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
SwapWeapons(pivotRPG);
activeWeapon = "RPG";
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
SwapWeapons(pivotPSM);
activeWeapon = "PSM";
}
#endregion
if(Input.GetButtonDown("Fire1"))
{
#region Raycasting
if (activeWeapon == "Pistol")
{
RaycastHit hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.TransformDirection(Vector3.for ward), out hit))
{
Debug.DrawRay(playerCam.transform.position, playerCam.transform.TransformDirection(Vector3.for ward) * hit.distance, Color.yellow);
if(hit.collider.gameObject.tag == "Pigeon")
{
hit.collider.gameObject.GetComponent<PigeonScript> ().DoDamage(10);
}
else
{
GameObject clonedBulletHole;
clonedBulletHole = Instantiate(bulletHole, hit.point, Quaternion.LookRotation(hit.normal));
clonedBulletHole.transform.parent = hit.collider.gameObject.transform;
}
}
}
#endregion
#region RPG
if (activeWeapon == "RPG")
{
dummyRocket.SetActive(false);
Rigidbody clonedRocket;
clonedRocket = Instantiate(rocket, rocketLP.position, rocketLP.rotation);
clonedRocket.velocity = rocketLP.TransformDirection(Vector3.forward * 10f);
StartCoroutine(ReloadRPG());
}
#endregion
if (activeWeapon == "PSM")
{
//PSM
}
}
}
void SwapWeapons(GameObject gunToShow)
{
iTween.RotateTo(pivotPistol, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(pivotRPG, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(pivotPSM, iTween.Hash(
"x", 70f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
iTween.RotateTo(gunToShow, iTween.Hash(
"x", 0f,
"time", .5f,
"islocal", true,
"easetype", "spring"
));
}
IEnumerator ReloadRPG()
{
yield return new WaitForSeconds(1f);
dummyRocket.SetActive(true);
}
}
Comment