- Home /
how do I shoot where my cross hair points?
I have a crosshair on the main camara and a fire function on my gun (zap) but when I fire the projectiles (standard Roll A Ball)it fires to right of the camera and right from where the crosshair points.
Crosshair function java (under Main Camera) var crosshairTexture : Texture2D; var position : Rect; static var OriginalOn = true;
function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() instead. { position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }
function OnGUI() { if(OriginalOn == true) { GUI.DrawTexture(position, crosshairTexture); } }
Fire function C# (under the gun)
using UnityEngine; using System.Collections;
public class fire : MonoBehaviour { public GameObject projectile; public float fireRate = 0.5F; private float nextFire = 0.0F;
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject as GameObject;
}
}
}
Please format your code correctly by highlighting the entire code and pressing the 101010 button.
Your answer
Follow this Question
Related Questions
Problem with AIM to SHOOT script 1 Answer
Aiming Gun at Cursor 2 Answers
How could I shoot a chemical in a fire extinguisher? 1 Answer
aim down sights of gun 3 Answers
How make Raycast Aim to Crosshair? 1 Answer