Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[B]FPS 1.13 raycast bullet holes[/B]

Discussion in 'Scripting' started by FirePlantGames, Feb 6, 2013.

  1. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    hey i just got a question is there a way to make the bullet hole a child of what it hits?
    this way it should follow the object it hits right?

    code:
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5.  
    6.  
    7. function Update ()
    8. {
    9. var hit : RaycastHit;
    10. if (Physics.Raycast(transform.position,transform.forward,hit,maxDist))
    11. {
    12. if (decalHitWall  hit.transform.tag == "Level Parts")
    13. Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    14. }
    15.     Destroy(gameObject);
    16. }
    17.  
     
  2. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Fixed. I think.
     
  3. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    no it didnt i got an error:canot convert blah blah
     
  4. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    var bulletHole = Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));

    I hate bottle feeding, but you have to instantiate it to the correct type, my guess is your doing it as a GameObject so make that line.

    var bulletHole = Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)) as GameObject;

    or

    var bulletHole = (GameObject) Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)) as GameObject;
     
  5. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    actually it can't convert:

    bulletHole.transform.parent = hit.transform.gameObject;

    EDIT: i fixed it with:
    Code (csharp):
    1.  
    2. var maxDist : float = 1000000000;
    3. var decalHitWall : GameObject;
    4. var floatInFrontOfWall : float = 0.00001;
    5.  
    6.  
    7.  
    8.  
    9.  
    10. function Update ()
    11. {
    12. var hit : RaycastHit;
    13. if (Physics.Raycast(transform.position,transform.forward,hit,maxDist))
    14. {
    15.  
    16. if (decalHitWall  hit.transform.tag == "Level Parts")
    17. {
    18. var bulletHole = Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    19. bulletHole.transform.parent = hit.transform;
    20. }
    21. }
    22. Destroy(gameObject);
    23. }
    24.  
     
    Last edited: Feb 9, 2013