- Home /
Help with Gui texture/Audio
Hey i have a gun script that works great but i need to transfer it from pc to android. Like instead of me clicking the scene i want it so when you press a gui texture, it fires, like on the pc version. But, i also need a gunshot sound to play when i touch the gui texture, as i shoot the gun.
Here is the gun script!
var Bullet : Transform; var Spawn : Transform;
function Update () { if(Input.GetButtonDown("Fire1")) { Shot(); } }
function Shot() { var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation); pel.rigidbody.AddForce(transform.forward * 10000);
}
Answer by Turbine · Aug 20, 2014 at 11:32 PM
An easy way is to use Unity's dreadful GUI system. I don't recommend it at all for your final release. It doesn't handle variable screen sizes by default so I'm going to leave that nugget for you.
public class Gun : MonoBehaviour
{
public Rect buttonPos;
public Texture buttonTexture;
public GUIStyle style;
void OnGUI()
{
if ( GUI.Button(buttonPos,buttonTexture, style) //both draws the button and checks if it's been clicked
&& Time.deltatime > 0 ) //and if the game isn't paused
{
Shoot();
}
}
void Shoot()
{
//stuff
}
}