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

Help needed! How to do this?

Discussion in 'Getting Started' started by iron_dizaster, Jul 17, 2020.

  1. iron_dizaster

    iron_dizaster

    Joined:
    Oct 27, 2018
    Posts:
    10
    I don't usually ask for help on forums like this, but this has been bugging me for several days now so I went here as a last resort. I have been looking at the unity manual aswell as online for solutions, but none of the things I tried seemed to work.

    Basically, I have a game where you control a ship, and that ship is automatically moving forward on a set path, but you can control the ship up, down, left, right. It explodes when touching obstacles, and fires lasers when holding space/LMB

    I have added a crosshair onto that ship which moves with the ship, so it can accurately display where the ship is gonna fire lasers, this is the crosshair code:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class CrossHairScript : MonoBehaviour
    8. {
    9.     public Image crossHair;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         ProcessCrosshairResting();
    15.     }
    16.  
    17.     private void ProcessCrosshairResting()
    18.     {
    19.         Vector3 crossHairPosition = Camera.main.WorldToScreenPoint(this.transform.position);
    20.         crossHair.transform.position = crossHairPosition;
    21.     }
    22.  
    23. }
    (This is the current working code for the crosshair without changing colors, the things I have tried before I have deleted and I am currently using this)

    I wanted to add a feature where if the crosshair is over an enemy ship, it would turn red.
    My first thought was to add a super long cube that was the length of the max distance of the laser, and the long cube would have a box collider and if it collided with an enemy ship, it would switch the current crosshair with the red one.

    I have tried many things such as making 2 public GameObject variables, and then I would use the .SetActive command if a collision was detected with an enemy ship (tag "Enemy")
    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class CrossHairScript : MonoBehaviour
    8. {
    9.     public GameObject crossHairResting;
    10.     public GameObject crossHairOnHover;
    11.  
    12.  
    13.     void OnCollisionEnter(Collision collision)
    14.     {
    15.         if (collision.gameObject.tag == "Enemy")
    16.         {
    17.             crossHairOnHover.SetActive(true);
    18.             crossHairResting.SetActive(false);
    19.             Vector3 crossHairOnHoverPosition = Camera.main.WorldToScreenPoint(this.transform.position);
    20.             crossHairOnHover.transform.position = crossHairOnHoverPosition;
    21.         }
    22.         else
    23.         {
    24.             crossHairOnHover.SetActive(false);
    25.             crossHairResting.SetActive(true);
    26.             Vector3 crossHairRestingPostion = Camera.main.WorldToScreenPoint(this.transform.position);
    27.             crossHairOnHover.transform.position = crossHairRestingPostion;
    28.         }
    29.     }
    30.  
    31. }
    The collisions however never worked, and neither did the crosshair in the scripts I tried to make. I thought it was because I didn't have a rigidbody on the long cube, nor was the box collider on trigger, however if I added a rigidbody or made it an on trigger, it would count as part of the ship and the ship would just immediately destroy itself once the scene was played. The closest I got it to working was when the ship exploded when the long cube touched an enemy, but it still didn't show the crosshairs at all.

    Also, I had the commands

    Code (CSharp):
    1.          Vector3 crossHairRestingPostion =                   Camera.main.WorldToScreenPoint(this.transform.position);
    2.             crossHairOnHover.transform.position = crossHairRestingPostion;
    In both the if statement and the else statement because I thought it would make them have the same position (which is probably terribly wrong, and one of the many reasons why it doesn't work)

    Can anyone please help me on this? I'm still fairly new to unity and C# in general but I am starting to pick up some pace, so I'm not a COMPLETE beginner, but still a noob. Any help would be greatly appreciated!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,963
    It sounds like you're having trouble detecting when the crosshair is over a ship. I would use a ray cast for that.

    Ray casts detect colliders, so you will need a collider on each ship. But it can detect both regular colliders and trigger colliders (there's an option for that — Physics.RayCast has lots of options), so use whichever you prefer.
     
    Joe-Censored likes this.
  3. iron_dizaster

    iron_dizaster

    Joined:
    Oct 27, 2018
    Posts:
    10
    Thanks for the advice. Will definitely try that out and fiddle with it and see if I can get it working.
     
    JoeStrout likes this.
unityunity