Hi!
I’m new to the Unity and I’m learning how to make a basic FPS. Right now, I’m stuck at making bullet holes have random rotation along the normal.
My code:
public float Cooldown = 0.1f;
float CooldownRemaining = 0;
public float Range = 100.0f;
public float BulletHoleOffset = 1.0f;
public GameObject debrisPrefab;
public GameObject BulletHole;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CooldownRemaining -= Time.deltaTime;
if (Input.GetMouseButton(0) && CooldownRemaining <= 0)
{
CooldownRemaining = Cooldown;
Ray ray = new Ray(Camera.main.transform.position + Camera.main.transform.forward, Camera.main.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Range))
{
Vector3 hitPoint = hit.point;
if (debrisPrefab != null)
{
//Particle effect
Instantiate(debrisPrefab, hitPoint, Quaternion.FromToRotation(Vector3.up, hit.normal));
//Bullet Hole
Instantiate(BulletHole, hitPoint + (hit.normal * BulletHoleOffset), Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
}
}
}
I’m stuck at “Quaternion.FromToRotation(Vector3.forward, hit.normal));” since I don’t know where to “plug” random rotation along the normal.
I managed to get a random rotation along particular axis, but it didn’t look good since it didn’t align with the normal.
Use Transform.Rotate, note that oyu can pass a parameter that tells the method to use local space (which is always the default value if you don’t pass the parameter). Rotate random degrees around the local y-axis and you are set
For the random scale, just pass a Vector3 composed of random values to the bullet Transform’s localScale variable.