Help with Time Crisis crosshair
Hi.
I'm making a game where you use the accelerometer to aim with a crosshair and GUI buttons to shoot, it's for mobile. The crosshair is an GUI image object with a Rigidbody to which i apply velocity given by the accelerometer.
I want to get it to work like in time crisis. If the crosshair is over the enemy and the player shoots, then it's an hit. What i'm currently trying to do is to raycast forward from the crosshairs RectTransform. But the ray is quite far away from where the crosshair is now when i shoot.
This is the code i'm using.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public Gun[] inventoryGuns;
public Gun currentGun;
private Transform crosshairPos;
private WaitForSeconds shotDuration = new WaitForSeconds(.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextfire;
// Use this for initialization
void Start () {
laserLine = GetComponent<LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
crosshairPos = GetComponentInParent<RectTransform> ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1") && Time.time > nextfire) {
nextfire = Time.time + currentGun.fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = crosshairPos.transform.position;
RaycastHit hit;
laserLine.SetPosition(0, crosshairPos.transform.position);
if(Physics.Raycast(rayOrigin, crosshairPos.transform.forward, out hit, 10000))
{
laserLine.SetPosition(1, hit.point);
}
else{
laserLine.SetPosition(1, rayOrigin + (crosshairPos.transform.forward * 10000));
}
Debug.Log(crosshairPos);
Debug.Log(laserLine);
}
}
}
First question, will this even be able to work like in time crisis? Seems to me now that the rays will never be able to hit enemies at the edge of the camera or how would i fix that?
Second, is there a simpler way to just compare what's behind the crosshair? No raycats, just checking behind the crosshair dot if there is an enemy.
Thanks in advance
Your answer
Follow this Question
Related Questions
Raycast help 0 Answers
Rigidbody blocking raycasts 2D 0 Answers
How do I get the default inspector for a CustomEditor for RectTransform? 2 Answers