- Home /
How do I make the bullet Tracer move towards the location of where the bullet hole prefab gets instantiated?
So I have this IEnumertator function called checkIfHit() that spawns two raycasts(one is exploratory and the other one instantiates a bullet hole if the raycast hits an enemy.) and a bullet tracer whenever the player presses the fire button. Here is the code for that function.
private IEnumerator checkIfHit()
{
Vector3 randomDirection = getRandomBulletDirection();
RaycastHit hit;
// Cast an exploratory raycast first from a randomly generated direction.
if(Physics.Raycast(bulletSpawnPoint.position, randomDirection, out hit, bulletRange))
{
// calculate how long it would take the bullet to get to its target.
float delay = hit.distance / bulletSpeed;
// calculate the bullet drop at the target.
Vector3 hitPoint = hit.point;
hitPoint.y -= delay * 9.8f;
// calculate the new vector's direction
Vector3 dir = hitPoint - bulletSpawnPoint.position;
yield return new WaitForSeconds(delay);
// spawn a bullet tracer from the direction(normalized) that was lowered to simulate a bullet drop.
StartCoroutine(spawnBulletTracer(dir.normalized));
// Now perform the actual shooting.
if (Physics.Raycast(bulletSpawnPoint.position, dir, out hit, bulletRange))
{
GameObject bulletHole = SpawningPool.CreateFromCache(bulletImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
yield return new WaitForSeconds(3.0f);
SpawningPool.ReturnToCache(bulletHole);
}
}
}
To simulate gun inaccuracy, I first generate a random direction vector using this function.
private Vector3 getRandomBulletDirection()
{
// Generate a random XY point inside a circle.
Vector3 randomDirection = Random.insideUnitCircle * scaleLimit;
randomDirection.z = spreadZDirection;
randomDirection = transform.TransformDirection(randomDirection.normalized);
return randomDirection;
}
After the exploratory raycast hits something and I've calculated the direction for the second raycast, I immediately call the function spawnBulletTracer to instantiate it and make it move in the direction that the second raycast is going to. Here is the code for that function.
private IEnumerator spawnBulletTracer(Vector3 direction)
{
// Retrieve from the spawn pool a cached bullet tracer object.
GameObject bulletTracer = SpawningPool.CreateFromCache(tracer, bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation);
Rigidbody rigidbodyTracer = bulletTracer.GetComponent<Rigidbody>();
rigidbodyTracer.velocity = transform.TransformDirection(direction * tracerSpeed);
yield return new WaitForSeconds(1.5f);
SpawningPool.ReturnToCache(bulletTracer, "Tracer");
// Send it back to the cache so that it may be used later.
}
If it helps, here is how my bulletTracer prefab looks like in the inspector.
To me, each step that I've done in each of those function made sense to me so maybe there is a miscalculation somewhere(more than likely it's the vector that I passing to spawnBulletTracer that is not correct.). At the very least each bullet hole is being instantiated in different locations which is what I want but the tracer is not moving in the direction that you would expect. Help would be appreciated.
Answer by Bieere · Aug 02, 2017 at 06:16 PM
What is the current behaviour, as it will help determine what the problem might be.
Does it not spawn at all, does it stop short, or move in an incorrect direction?
The behavior that I want is not only for the bullethole to appear in random locations(to simulate bullets firing in different directions) but for the bullet tracer to also going in the correct direction. What I mean by correct direction is for example, if I shoot at the center of the screen and let us also say that I get generate a random vector that points to the center of the screen, then the bullethole prefab should be instantiated in the center screen and the tracer should move to the center of the screen as well.
The current behavior is the bullethole instantiates in a random location but the tracer is not moving in the correct direction(If the bullethole instantiates a little bit to the left, the bullet tracer might move to the right).
Answer by Deathslice · Aug 02, 2017 at 08:43 PM
I solved my problem. Instead of doing
rigidbodyTracer.velocity = transform.TransformDirection(direction * tracerSpeed);
I changed it to
rigidbodyTracer.velocity = direction * tracerSpeed;
I not sure why it's working now. Maybe somebody here can explain why.
Your answer
Follow this Question
Related Questions
Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers
Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer
instantiate an object on the intersection point between two objects on the x and y axis 1 Answer
Shoot Raycast to a specified Angle 0 Answers
Apply rigid body force in opposite direction of collider 1 Answer