I’m having trouble with the Raycasting collision detection,
I have created a prefab that casts a ray, and if it hits a wall it will instantiate a decal on the given hit.point
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ThreeEyedGames;
using UnityEngine.Rendering;
public class Rob_Blood_Test : MonoBehaviour {
// Use this for initialization
public GameObject decalPrefab;
public float raydistance = 0.5f;
public Material[] bloodSplats;
//public Eas
void Start ()
{
Destroy(this.gameObject,1.5f);
}
// Update is called once per frame
void Update ()
{
LayerMask layerMask = (1 << 10 | 1 << 9);
RaycastHit hit;
if (Physics.Raycast(transform.position,transform.forward, out hit, raydistance, layerMask))
{
if (hit.collider.tag != "Enemy" && hit.collider.tag != "Player")
{
GameObject decal = Instantiate(decalPrefab, hit.collider.transform, true);
decal.GetComponent<ThreeEyedGames.Decal>().Material = bloodSplats[Random.Range(0, bloodSplats.Length)];
decal.transform.position = hit.point;
decal.transform.up = hit.normal;
decal.transform.Rotate(Vector3.up, Random.Range(0, 360), Space.Self);
Destroy(this.gameObject);
}
}
Debug.DrawRay(transform.position, transform.forward * raydistance, Color.green);
}
private void ChangeMaterialIndex()
{
if (bloodSplats.Length == 0) { return; }
int i = Random.Range(0, bloodSplats.Length - 1);
Material randomMat = bloodSplats[i];
}
}
This works fine, until it is moving too fast using
GameObject go = Instantiate(bloodSpawnTest, new Vector3(Random.Range(transform.position.x - 1.0f, transform.position.x + 1.0f),transform.position.y, Random.Range(transform.position.z - 1.0f, transform.position.z + 1.0f)), transform.rotation) as GameObject;
go.GetComponent<Rigidbody>().AddForce(transform.forward * Random.Range(5,10), ForceMode.Impulse);
The Rigidbody is set to continuous Dynamic and it is still clipping through walls and only working 10% of the time,
Is there any other ways I can make the collision detection more reliable?