The bullet hole colliding with player and "Triggers"
Hi, I have a big problem with my bullet holes. When the player is shooting, gun instantiates the bullet hole on the point where raycasing is hitting and it is workin fine. But when the player is moving fast or jumping and shooting, those bullet holes instantiates sometimes on him and it does not look well.
Another problem is with triggers.
I have some objects on scene with Trigger collider and the bullets are colliding with it too. The result of it looks at least funny but it still a bug,
Can you help me solve this problem? Here are scripts :
Recoil script : using UnityEngine; using System.Collections;
public class Recoil : MonoBehaviour {
private float recoil = 0.0f;
private float maxRecoil_x = 20f;
private float maxRecoil_y = 20f;
private float recoilSpeed = 2f;
public void StartRecoil(float recoilParam, float maxRecoil_xParam, float recoilSpeedParam)
{
// in seconds
recoil = recoilParam;
maxRecoil_x = maxRecoil_xParam;
recoilSpeed = recoilSpeedParam;
maxRecoil_y = Random.Range(-1f,1f);
}
public void recoiling()
{
if (recoil > 0f)
{
Quaternion maxRecoil = Quaternion.Euler(maxRecoil_x, maxRecoil_y, 0f);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed);
recoil -= Time.deltaTime;
}
else {
recoil = 0f;
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, Time.deltaTime * recoilSpeed / 2);
}
}
// Update is called once per frame
void Update()
{
recoiling();
}
}
shooting script : using UnityEngine; using System.Collections; using System.Threading;
public class Shooting : MonoBehaviour
{
[Header("Gameobjects")]
public GameObject ammo;
private Ammo ammunition;
public GameObject bullethole;
public GameObject gun;
public GameObject recoil;
public Recoil recl;
AudioSource shoots;
//public GameObject fireaftershoot;
[Header("speed and time")]
public Ppauser pauzer;
public float speed = 30f;
public float timer = 0.0f;
public float maxtimer = 0.5f;
void Start()
{
ammunition = ammo.GetComponent<Ammo>();
Cursor.visible = true;
Cursor.lockState = CursorLockMode.Locked;
shoots = GetComponent<AudioSource>();
recl = recoil.GetComponent<Recoil>();
}
void FixedUpdate()
{
float randomrecoilauto = Random.Range(-5, -1);
float randomrecoilsingle = Random.Range(-5, -1);
Vector3 betterhole = new Vector3(0, 0, 0.00002f);
timer += Time.deltaTime;
if (Input.GetMouseButtonDown(0) && pauzer.paused == false && timer >= maxtimer && ammunition.ammoInclip != 0 && ammunition.semiOrAuto == false) // strzelam w trybie semi
{
recl.StartRecoil(0.2f, randomrecoilsingle, 20f); // start recoil
ammunition.ammoInclip -= 1; // takes the ammo from clip
shoots.Play();
timer = 0.0f;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 test = ray.origin;
//Instantiate(fireaftershoot, transform.position, gun.transform.rotation);
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 10)) // obliczam punkt trafienia
{
Debug.DrawLine(test, hit.point, Color.blue, 2);
GameObject hole = Instantiate(bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject; //strzelam i tworze dziure po kuli
}
}
else if (Input.GetMouseButton(0) && pauzer.paused == false && timer >= 0.1f && ammunition.ammoInclip != 0 && ammunition.semiOrAuto == true) //strzelam w trybie auto
{
recl.StartRecoil(0.2f, randomrecoilauto, 20f); // start recoil
ammunition.ammoInclip -= 1; // takes the ammo from clip
shoots.Play();
timer = 0.0f;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 test = ray.origin;
//Instantiate(fireaftershoot, transform.position, gun.transform.rotation);
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 10))
{
Debug.DrawLine(test, hit.point, Color.blue, 2);
GameObject hole = Instantiate(bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
}
}
}
}
I've already tried to fix it, but it didn't worked, anyway , here's the code of the "fixer":
using UnityEngine;
using System.Collections;
public class BulletIgnoreCollision : MonoBehaviour {
public GameObject player;
public GameObject trigger1;
public GameObject trigger2;
public GameObject trigger3;
public GameObject trigger4;
Collider playercol;
Collider trigger1col;
Collider trigger2col;
Collider trigger3col;
Collider trigger4col;
Collider bullethole;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player") ;
trigger1 = GameObject.FindGameObjectWithTag("Triggers");
trigger2 = GameObject.FindGameObjectWithTag("Triggers2");
trigger3 = GameObject.FindGameObjectWithTag("Triggers3");
trigger4 = GameObject.FindGameObjectWithTag("Triggers4");
playercol = player.GetComponent<Collider>();
trigger1col = trigger1.GetComponent<Collider>();
trigger2col = trigger2.GetComponent<Collider>();
trigger3col = trigger3.GetComponent<Collider>();
trigger4col = trigger4.GetComponent<Collider>();
bullethole = GetComponent<Collider>();
}
// Update is called once per frame
void Update ()
{
Physics.IgnoreCollision(playercol, bullethole );
Physics.IgnoreCollision(trigger1col, bullethole);
Physics.IgnoreCollision(trigger2col, bullethole);
Physics.IgnoreCollision(trigger3col, bullethole);
Physics.IgnoreCollision(trigger4col, bullethole);
}
}
Your answer
Follow this Question
Related Questions
Procedurally generating cubes 1 Answer
Interesting Enemy AI issue 2 Answers
Accessing the script of an instantiated object 1 Answer
Instantiate a Prefab along the surface of a sphere. C# 0 Answers