Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Shooting to the center of the screen.

Discussion in 'Scripting' started by oen432, Apr 25, 2015.

  1. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    39
    Hi,

    This code is used to fire laser straight forward.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LeftGun : MonoBehaviour {
    5.  
    6.     public Rigidbody laser;
    7.     public AudioClip LaserShot;
    8.  
    9.     float shotSpeed = 0.1f;
    10.     bool canShoot = true;
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         if(!Engine.escape)
    16.         {
    17.             shotSpeed -= Time.deltaTime;
    18.  
    19.             if(shotSpeed <= 0f)
    20.                 canShoot = true;
    21.  
    22.             if(Input.GetButton("Fire1") && canShoot)
    23.             {
    24.                 shotSpeed = 0.1f;
    25.                 canShoot = false;
    26.                 PlayerGUI.ammunition--;
    27.                 audio.PlayOneShot(LaserShot);
    28.                 Rigidbody clone = Instantiate(laser,transform.position, transform.rotation) as Rigidbody;
    29.                 clone.velocity = transform.TransformDirection(-80, 0, 0);
    30.                 Destroy(clone.gameObject, 3);
    31.             }
    32.         }
    33.     }
    I would like to fire to the center of the screen (where crosshair is). How can I achive that?
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    So, you want to fire a laser on a 2D plane toward a crosshair that is always located at the center of the screen. Is that correct?
     
  3. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    39
    Something like this:
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,412
    I'd do something like this:

    Code (csharp):
    1. //
    2. // Screen space (viewport) coordinates are between 0..1 so 0.5 on both the x and y is
    3. // the screen centre.
    4. //
    5. Vector3 screenSpaceCenter = new Vector3(0.5f, 0.5f, 0);
    6. //
    7. // Convert that to a world point where myCamera needs to be changed to be
    8. // your actual camera.
    9. //
    10. Vector3 laserEnd = myCamera.ViewportToWorldPoint(screenSpaceCenter);
    11.  
    12. //
    13. // Shoot towards laserEnd
    14. //
     
    hamsterbytedev likes this.
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    That solution will be adequate if the projectiles always go to the center of the screen and the spaceship doesn't move. If it does move and the lasers go to a point in front of it rather than a static position at the center of the screen that solution will not work.
     
  6. oen432

    oen432

    Joined:
    Apr 24, 2015
    Posts:
    39
    Yeah... It's not working. Like outwarddesign said, spaceship is moving and it's not working.

    @Edit
    Solved.
    Code (CSharp):
    1. public Camera myCamera;
    2.  
    3. float x = Screen.width / 2;
    4. float y = Screen.height / 2;
    5.              
    6. var ray = myCamera.ScreenPointToRay(new Vector3(x, y, 0));
    7. clone.velocity = ray.direction * 80;
     
    Last edited: Apr 25, 2015
    hamsterbytedev likes this.
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Looks good. Good job!
     
  8. hammadzahidfinz

    hammadzahidfinz

    Joined:
    Jun 25, 2018
    Posts:
    6
    thanks worked
     
unityunity