Search Unity

ScreenToWorldPoint Mouse Click not Functional at a 45 degree angle

Discussion in 'Scripting' started by MikielSG, Jan 9, 2020.

  1. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    47
    Hello,

    I am experiencing an issue with the following scripts. I am intermediate at scripting (so pardon any mistakes in the code). The following scripts work correctly when the camera is at a 90 degree angle, however when i shift that angle to 45 degree's, the script will only fire on the x axis. I want y Axis to be 0, because my game will not enable people to move up and down. As i understand, I need the z axis to do this, however Screen to world does not return z Values.

    This is a unity 3d game, but my movement will only be on the x/z axis (assuming im explaining that correctly, my ship will only move left right forward and back, not up in down, resulting in a 3d space, with 2d movement (if that makes anysense).

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class FireToMouse : MonoBehaviour
    5. {
    6.     public Attributes primary;
    7.  
    8.     public GameObject fp1;
    9.     public GameObject fp2;
    10.     public GameObject fp3;
    11.     public GameObject fp4;
    12.     public GameObject fp5;
    13.     public GameObject Bullet;
    14.     public GameObject LaserImpactGFX;
    15.     public GameObject chargeGFX;
    16.  
    17.     public float shootTimer = 0.5f;
    18.     public float timeBetweenShots = 0.5f;
    19.     public float CoolDown = 0.5f;
    20.     public int energycost = 25;
    21.     public float WeaponsPause = 3;
    22.     public float ReloadTime = 3;
    23.     public int MinDamage = 50;
    24.     public int MaxDamage = 100;
    25.  
    26.     public bool cannonsActive;
    27.     public bool lasersActive;
    28.     public bool missilesActive;
    29.     public bool CanFire = true;
    30.     public bool missiles;
    31.     public bool cannons;
    32.     public bool lasers;
    33.  
    34.     public int shots;
    35.  
    36.     public Vector3 target;
    37.  
    38.  
    39.     public LineRenderer line;
    40.  
    41.     public void Start()
    42.     {
    43.         line.enabled = false;
    44.         chargeGFX.transform.gameObject.SetActive(false);
    45.         LaserImpactGFX.transform.gameObject.SetActive(false);
    46.     }
    47.     public void Update()
    48.     {
    49.         target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
    50.         target.y = 0f;
    51.         Fireatmouse();
    52.         FireCannons(WeaponsPause);
    53.         FireLaser(WeaponsPause);
    54.         FireMissile(WeaponsPause);
    55.  
    56.         if (line.enabled == true)
    57.         {
    58.             line.SetPosition(0, new Vector3(fp1.transform.position.x, fp1.transform.position.y, fp1.transform.position.z));
    59.             line.SetPosition(1, new Vector3(target.x, target.y, target.z));
    60.             chargeGFX.transform.gameObject.SetActive(true);
    61.  
    62.         }
    63.         else
    64.         {
    65.             line.enabled = false;
    66.             chargeGFX.transform.gameObject.SetActive(false);
    67.             LaserImpactGFX.transform.gameObject.SetActive(false);
    68.         }
    69.  
    70.     }
    71.  
    72.     public void Fireatmouse()
    73.     {
    74.             CoolDown -= Time.deltaTime;
    75.             if (CoolDown <= 0)
    76.             {
    77.                 CoolDown = 0;
    78.             }
    79.  
    80.             if (CoolDown <= 0)
    81.             {
    82.                 CanFire = true;
    83.             }
    84.             else
    85.             {
    86.                 CanFire = false;
    87.                 if (CanFire == false)
    88.                 {
    89.                     return;
    90.                 }
    91.             }
    92.  
    93.              CoolDown = ReloadTime;
    94.  
    95.             //turn on cannons trigger
    96.             if (Input.GetMouseButton(0) && cannons && CanFire == true)
    97.             {
    98.                 StartCoroutine("FireCannons", WeaponsPause);
    99.             }
    100.  
    101.             //turn on laser trigger
    102.             if (Input.GetMouseButton(0) && lasers)
    103.             {
    104.                 lasersActive = true;
    105.             }
    106.  
    107.             //shutoff weapons
    108.             if (Input.GetMouseButton(0) == false)
    109.             {
    110.                 cannonsActive = false;
    111.                 lasersActive = false;
    112.             }
    113.  
    114.             //start laser coroutine
    115.             if (lasersActive == true)
    116.             {
    117.              StartCoroutine("FireLaser", WeaponsPause);
    118.                 line.enabled = true;
    119.             }
    120.                 else if (lasersActive == false)
    121.                 {
    122.                     line.enabled = false;
    123.                 }
    124.  
    125.             if (missiles == true)
    126.             {
    127.                 StartCoroutine("FireMissile", WeaponsPause);
    128.             }
    129.          
    130.  
    131.     }
    132.  
    133.  
    134.     public IEnumerator FireLaser(float WeaponsPause)
    135.     {
    136.  
    137.         RaycastHit hit;
    138.         if (Physics.Raycast(fp1.transform.position, target - fp1.transform.position, out hit))
    139.         {
    140.  
    141.             switch (hit.transform.gameObject.tag)
    142.             {
    143.                 case "NPC":
    144.                     {
    145.                        
    146.                         LaserImpactGFX.transform.gameObject.SetActive(true);
    147.                         LaserImpactGFX.transform.position = target;
    148.                         hit.transform.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
    149.                         yield return new WaitForSeconds(5);
    150.                         //creates an graphics effect for where the laser is hitting.
    151.                         break;
    152.                     }
    153.  
    154.                 case "Asteroids":
    155.                     {
    156.  
    157.                         LaserImpactGFX.transform.gameObject.SetActive(true);
    158.                         LaserImpactGFX.transform.position = target;
    159.                         hit.transform.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
    160.                         yield return new WaitForSeconds(5);
    161.                         //creates an graphics effect for where the laser is hitting.
    162.                         break;
    163.                     }
    164.             }
    165.         }
    166.         // turns off the laser when completed
    167.         //FireSFX.Stop();
    168.         line.enabled = false;
    169.         StopCoroutine("FireLaser");
    170.  
    171.     }
    172.  
    173.  
    174.     public IEnumerator FireCannons(float WeaponsPause)
    175.     {
    176.  
    177.         shots = Random.Range(1, 5);
    178.  
    179.         if (shots == 1)
    180.         {
    181.             primary.CurrentEnergy -= energycost;
    182.             GameObject b = GameObject.Instantiate(Bullet, fp1.transform) as GameObject;
    183.             b.transform.parent = null;
    184.             yield return new WaitForSeconds(WeaponsPause);
    185.         }
    186.         else if (shots == 2)
    187.         {
    188.             primary.CurrentEnergy -= energycost;
    189.             GameObject b = GameObject.Instantiate(Bullet, fp2.transform);
    190.             b.transform.parent = null;
    191.             yield return new WaitForSeconds(WeaponsPause);
    192.         }
    193.  
    194.         else if (shots == 3)
    195.         {
    196.             primary.CurrentEnergy -= energycost;
    197.             GameObject b = GameObject.Instantiate(Bullet, fp3.transform);
    198.             b.transform.parent = null;
    199.             yield return new WaitForSeconds(WeaponsPause);
    200.  
    201.         }
    202.         else if (shots == 4)
    203.         {
    204.             primary.CurrentEnergy -= energycost;
    205.             GameObject b = GameObject.Instantiate(Bullet, fp4.transform);
    206.             b.transform.parent = null;
    207.             yield return new WaitForSeconds(WeaponsPause);
    208.  
    209.         }
    210.         else if (shots == 5)
    211.         {
    212.             primary.CurrentEnergy -= energycost;
    213.             GameObject b = GameObject.Instantiate(Bullet, fp5.transform);
    214.             b.transform.parent = null;
    215.             yield return new WaitForSeconds(WeaponsPause);
    216.  
    217.         }
    218.  
    219.         if (shots >= 5)
    220.         {
    221.             shots = 5;
    222.         }
    223.  
    224.         if (shots <= 1)
    225.         {
    226.             shots = 1;
    227.         }
    228.  
    229.         yield return new WaitForSeconds(WeaponsPause);
    230.         StopCoroutine("FireCannons");
    231.  
    232.  
    233.     }
    234.     public IEnumerator FireMissile(float WeaponsPause)
    235.     {
    236.  
    237.         shots = Random.Range(1, 5);
    238.  
    239.         if (shots == 1)
    240.         {
    241.             primary.CurrentEnergy -= energycost;
    242.             GameObject b = GameObject.Instantiate(Bullet, fp1.transform) as GameObject;
    243.             b.transform.parent = null;
    244.             yield return new WaitForSeconds(WeaponsPause);
    245.         }
    246.         else if (shots == 2)
    247.         {
    248.             primary.CurrentEnergy -= energycost;
    249.             GameObject b = GameObject.Instantiate(Bullet, fp2.transform);
    250.             b.transform.parent = null;
    251.             yield return new WaitForSeconds(WeaponsPause);
    252.         }
    253.  
    254.         else if (shots == 3)
    255.         {
    256.             primary.CurrentEnergy -= energycost;
    257.             GameObject b = GameObject.Instantiate(Bullet, fp3.transform);
    258.             b.transform.parent = null;
    259.             yield return new WaitForSeconds(WeaponsPause);
    260.  
    261.         }
    262.         else if (shots == 4)
    263.         {
    264.             primary.CurrentEnergy -= energycost;
    265.             GameObject b = GameObject.Instantiate(Bullet, fp4.transform);
    266.             b.transform.parent = null;
    267.             yield return new WaitForSeconds(WeaponsPause);
    268.  
    269.         }
    270.         else if (shots == 5)
    271.         {
    272.             primary.CurrentEnergy -= energycost;
    273.             GameObject b = GameObject.Instantiate(Bullet, fp5.transform);
    274.             b.transform.parent = null;
    275.             yield return new WaitForSeconds(WeaponsPause);
    276.  
    277.         }
    278.  
    279.         if (shots >= 5)
    280.         {
    281.             shots = 5;
    282.         }
    283.  
    284.         if (shots <= 1)
    285.         {
    286.             shots = 1;
    287.         }
    288.  
    289.         yield return new WaitForSeconds(WeaponsPause);
    290.         StopCoroutine("TurretFire");
    291.  
    292.  
    293.     }
    294.  
    295. }
    296.  
    297. [code=CSharp]using UnityEngine;
    298.  
    299. public class BulletScript : MonoBehaviour
    300. {
    301.     FireToMouse ftm;
    302.     public float speed = 350f;
    303.     public int MinDamage = 100;
    304.     public int MaxDamage = 200;
    305.     public GameObject ImpactGFX;
    306.     public AudioSource AudioSFXImpact;
    307.     GameObject bullet;
    308.     public GameObject muzzleGFX;
    309.     public float duration = 3f;
    310.     public float autoDestruct = 1;
    311.     Vector3 target;
    312.     Vector3 dir;
    313.  
    314.  
    315.     // Start is called before the first frame update
    316.     void Start()
    317.     {
    318.         GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    319.         target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
    320.         target.y = 0f;
    321.         bullet = this.gameObject;
    322.         transform.LookAt(target);
    323.     }
    324.  
    325.     // Update is called once per frame
    326.     void Update()
    327.     {
    328.         float step = speed * Time.deltaTime;
    329.  
    330.  
    331.    
    332.         Vector3 dir = transform.forward * 100 * step;
    333.  
    334.         if (Vector3.Distance(this.transform.position, target) > autoDestruct)
    335.         {
    336.             transform.Translate(dir.normalized * step, Space.World);
    337.         }
    338.         else if (Vector3.Distance(this.transform.position, target) <= autoDestruct)
    339.         {
    340.  
    341.             Destroy(this.gameObject);
    342.         }
    343.        
    344.  
    345.         duration -= Time.deltaTime;
    346.  
    347.         if (duration <= 0)
    348.         {
    349.             Destroy(bullet);
    350.         }
    351.  
    352.         if(Vector3.Distance(bullet.transform.position, target) <= autoDestruct)
    353.         {
    354.            
    355.         }
    356.  
    357.     }
    358.     void OnDrawGizmosSelected()
    359.     {
    360.         Gizmos.color = Color.red;
    361.         Gizmos.DrawWireSphere(transform.position, autoDestruct);
    362.     }
    363.  
    364.     void ApplyForce(Rigidbody bullet)
    365.     {
    366.  
    367.  
    368.     }
    369.  
    370.     void OnTriggerEnter(Collider collision)
    371.     {
    372.  
    373.         if (collision.gameObject.tag == "NPC")
    374.         {
    375.             collision.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
    376.             Destroy(this.bullet);
    377.         }
    378.  
    379.         if (collision.gameObject.tag == "Asteroids")
    380.         {
    381.             collision.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
    382.             Destroy(this.bullet);
    383.         }
    384.     }
    385. }
    386.  

    [/code]
     
  2. MikielSG

    MikielSG

    Joined:
    Dec 26, 2016
    Posts:
    47
    Still looking for help on this problem!