Search Unity

How to fire a bullet in direction to a reticle

Discussion in 'Scripting' started by edufissure, Jan 18, 2019.

  1. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    I have a prefab object called FireballMainCharacter, which is a rigidbody sphere with this script attached:

    Code (CSharp):
    1.  
    2.     public class FireballMainCharacter : MonoBehaviour {
    3.          // Use this for initialization
    4.          public float speed=10.0f;
    5.          public int damage=1;
    6.          // Update is called once per frame
    7.          void Update () {
    8.              // Move the object forward along its z axis speed*unit/second.
    9.          //this.gameObject.transform.Translate(0, 0, speed * Time.deltaTime);
    10.          this.gameObject.transform.Translate(0, 0, speed * Time.deltaTime);
    11.          }
    12.          //Called when another object collides with this trigger
    13.          void OnTriggerEnter( Collider other){
    14.            
    15.              PlayerCharacterElyEnemy player= other.GetComponent<PlayerCharacterElyEnemy>();
    16.            
    17.              //Check if other object is PlayerCharacter
    18.              if( player!= null) {
    19.                      Debug.Log("ElyEnmy: llamo a hurt" + player.name);
    20.                      player.Hurt(damage);
    21.                      //player.transform.Rotate(-75, 0, 0);
    22.                  }
    23.          //    Debug.Log("Other hit" + other.name);
    24.     //        player.transform.Rotate(-75, 0, 0);
    25.              //IMPORTANT: We have to destroy the fireball, once it has been collided with other object....before check with objects
    26.              Destroy(this.gameObject);
    27.          } // End OnTriggerEnd
    28.        
    29.     }
    Know i have the main character with a GunEnd ( the point from i want to fire). I want to fire to a crosshair or reticle which is in half screen ( 3d game). The start point is good, but i can get the fireball to go to the reticle, the objectPoint


    1. Code (CSharp):
      1.     void Update () {
      2.              //Add && cam.name=Shooting camera to only shoot from shoot camera... && System.String.Equals(_camera.name,"ShootCamera")
      3.              if( Input.GetMouseButtonDown(0) && _camera.enabled == true )
      4.              {
      5.            
      6.                  Vector3 startPoint = gunHolder.transform.position;
      7.                  Vector3 objectpoint = new Vector3( _camera.pixelWidth/2, _camera.pixelHeight/2, 0);
      8.                
      9.                  _fireball = Instantiate(fireballPrefab) as GameObject;
      10.                
      11.                  //Place the fireball in front of the enemy and point in the same direction
      12.                  Vector3 directionToShoot = objectpoint-startPoint;
      13.                  _fireball.transform.position = startPoint;
      14.                  _fireball.transform.rotation = transform.rotation;
      15.              }
    I cant get directionToShoot work as expected, now ive get that the fireball begins in the gunEnd ( gunHolder) which its ok, but it goes straight ahead not in the direction to the reticle.

    Any help would be apreciated. The script i get as public variable the character, and the gunEnd and is attached to the ShootCamera



     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Your calculation of objectpoint is the issue; right now, it will always give the same world-space point. Instead, you'll want to do a raycast to find an object to shoot at.
    Code (csharp):
    1. Vector3 startPoint = gunHolder.transform.position;
    2. Vector3 objectpoint;
    3. RaycaastHit hit;
    4. if(Physics.Raycast(_camera.transform.position, _camera.transform.forward, out hit, 100))
    5.    objectpoint = hit.point;
    6. else
    7.    objectpoint = _camera.transform.position + camera.transform.forward * 100;
    8.                
    9. _fireball = Instantiate(fireballPrefab) as GameObject;
    10.                
    11. //Place the fireball in front of the enemy and point in the same direction
    12. Vector3 directionToShoot = objectpoint-startPoint;
    13. _fireball.transform.position = startPoint;
    14. _fireball.transform.rotation = transform.rotation;
     
  3. edufissure

    edufissure

    Joined:
    Oct 25, 2018
    Posts:
    66
    Thanks for the help is very close but didnt work, perhaps its a mistake because you dont use the objectpoint nor the directionShoot.... you only use the direction shoot as i guess...

    Here is a demo whats happening:
    https://drive.google.com/open?id=1ChibWZwoct-qbMVzw7DLa-goenqqmmO8