Search Unity

GUI Texture button tied to fire action

Discussion in 'Scripting' started by Unitizer, Apr 6, 2009.

  1. Unitizer

    Unitizer

    Joined:
    Apr 6, 2009
    Posts:
    3
    Hi, for some reason I am having trouble wrapping my mind around this problem. Basically what I have is a list of buttons. I want each button to cause a prefab to fire forwards. I have a third person camera and character running flawlessly. As of right now when I push one of the buttons, it fires the prefab from its original location in the scene and has no relation to the character.

    How do I tie the fire prefab script to the character object? I want to have the objects always shoot out from in front of the camera.

    Thanks
    Unitizer
     
  2. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    An easy way you could fix this problem, is set a "Target" transform for your GUI script. Then from the fire button's code, you could say...

    Instantiate( BulletPrefab, Target.position, Target.rotation );

    this would effectively create the prefab you want at the player location and not just at the coordinates of the Gameobject or the world center.
     
  3. Unitizer

    Unitizer

    Joined:
    Apr 6, 2009
    Posts:
    3
    Thanks, with your help the objects now originate correctly from my characters location. There is a new problem however. The objects still only shoot the direction that they were facing. So if I point my character in that original direction they shoot fine. If I am in the opposite direction the character gets shot in the face basically because they are still trying to travel in the original direction. There must be a way to have the objects always shoot from the character out. I just don't know what would do it.

    Code (csharp):
    1.  
    2.  function OnMouseDown()
    3.  {
    4.     state++;
    5.     if (state == 2)
    6.         myGUITexture.texture = pressedTexture;
    7.        
    8.  
    9. var Box : Rigidbody = Instantiate(myPrefab, Target.position, Target.rotation);
    10. Box.velocity = transform.TransformDirection (Vector3.forward * 25);
    11.            
    12.         }
    13.     }
    14.  
    15.  
    I am guessing it has something to do with the transform direction.

    Thanks