Search Unity

How to Rotate Sparks Based on the Direction Fired? (Prefab shooting not raycast)

Discussion in 'Scripting' started by mikemac30, Oct 19, 2017.

  1. mikemac30

    mikemac30

    Joined:
    Oct 13, 2017
    Posts:
    2
    Hi,
    I'm making an FPS game at the moment. I've run into a problem. If I fire a bullet at something I want to instantiate a sparks prefab and then rotate in the direction it was fired. (I am using prefab shooting not raycast) I already know how to instantiate the spark prefab, that part is easy. But how do I change the rotation of the prefab to match the direction it was fired?

    Thanks a ton

    Also I could't find any topics that could help me but if anyone sees any please point me in that direction.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    The easiest way to match an object's rotation to look in a direction is to set the .forward property of the transform.

    So if you're shooting in the direction shootDir, you just:

    Code (csharp):
    1. var sparks = Instantiate(sparksPrefab);
    2. sparks.transform.position = whatever;
    3. sparks.transform.forward = shootDir;
     
  3. mikemac30

    mikemac30

    Joined:
    Oct 13, 2017
    Posts:
    2
    Thanks for replying @Baste
    So Sorry I didn't really give enough info.
    I'm instantiating a bullet prefab, setting it's parent to none, and adding rigidbody force.
    The bullet has a script attached that uses OnTriggerEnter to detect if it has hit somthing.
    If it does hit something it instantiates another prefab, sparks, sets its parent to null and destroys itself.

    I want sparks to face out towards the player. When I shoot at a wall the sparks appear going up (as if I had shot at the ground) Is there some way to tell which way a collision came from? If I had that I think I could find my way from there pretty easily.
     
  4. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    If you use OnCollisionEnter, it gives you a Collision, which contains a list of ContactPoints, which have a "normal" property - that would be the direction facing out from the struck surface.

    With OnTriggerEnter you don't have that directly, but you could estimate it by using the ClosestPoint method of the other collider:
    Code (csharp):
    1. var estimatedNormal = (transform.position - other.ClosestPoint(transform.position)).normalized;
    This will fail if your object penetrated so far in a single frame that its center is inside the other object. If you know the velocity you were moving at, you can check for that case.
     
    mikemac30 likes this.
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    The same thing applies, you set the instantiates thing's .forward direction to wherever you want it to point.

    If you want it to face the opposite direction of the bullet, you can simply set it to -bullet.transform.forward. If you want it to face the player, you set it to (bullet.position - player.position).normalized, although that'll probably look weird? If you want it to face away from the wall, use what Errorsatz is saying.
     
    mikemac30 likes this.