How can I incorporate a cross-hair system into Argon Assault?

In Argon Assault, I want to add a dynamic cross-hair into the game, but I’m not sure how.
Here’s some gameplay:
(Sorry for the format and low quality…)
https://watch.screencastify.com/v/hswQgOrxccfyoIZdkGQe

Here’s my code for the Player Controller:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour 
{
    
    [Header ("General")]
    [Tooltip("In ms^-1")] [SerializeField] float Speed = 30f;
    [Tooltip("In meters")] [SerializeField] float xRange = 16f;
    [Tooltip("In meters")] [SerializeField] float yRange = 10f;
    [SerializeField] GameObject[] guns;

    [Header ("Position Based Parameters")]
    [SerializeField] float positionPitchFactor = -2f;
    [SerializeField] float positionYawFactor = 1.5f;

    [Header ("Control Based Parameters")]
    [SerializeField] float controlRollFactor = -30f;
    [SerializeField] float controlPitchFactor = -2f;

    float xThrow, yThrow;
    bool isControlEnabled = true;

    // Start is called before the first frame update
    void Start()
    {

    }

 
    // Update is called once per frame
    void Update()
    {
        if (isControlEnabled)
        {
            ProcessTranslation();
            ProcessRotation();
            ProcessFiring();
        }
    }

    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControl = yThrow * controlPitchFactor;
        float pitch = pitchDueToPosition + pitchDueToControl;
        
        float yaw = transform.localPosition.x * positionYawFactor;
        
        float roll = xThrow * controlRollFactor;
        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    private void ProcessTranslation()
    {
        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        float xOffset = xThrow * Speed * Time.deltaTime;

        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        yThrow = CrossPlatformInputManager.GetAxis("Vertical");
        float yOffset = yThrow * Speed * Time.deltaTime;

        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);

        
    }

    void OnPlayerDeath()
    {
        isControlEnabled = false;
    }

    public void ProcessFiring()
    {
        if (CrossPlatformInputManager.GetButton("Fire1"))
        {
            ActiveGuns();
        }
        else
        {
            DeactivateGuns();
        }

    }

    void ActiveGuns()
    {
        foreach (GameObject gun in guns)
        {
            gun.SetActive(true);
        }
    }

    void DeactivateGuns()
    {
        foreach (GameObject gun in guns)
        {
            gun.SetActive(false);
        }
    }
}

I do not have a cross-hair asset, or know how to make one, either. I have found many cross-hairs on the Unity asset store, so I’m sure I’ll just use one of those!

Putting a cross hair is relatively simple endeavour… Just put a UI Image element in the middle of the screen (or off centered a little if appropriate)…

For this game though, it’s more of a third person game that doesn’t typically lend itself to this method to work since you’re not looking straight ahead… aka, down the barren of the gun. So you’d first need to figure out how it’s going to behave to have a 2d cross-hair in this kind of 3d environment.

That all being said, part of the journey is to experiment and learn.

1 Like

Okay, I think I can figure it out from here!

I implemented a crosshair in my Argon Assault version called Skies Over Norville Valley, that you can check. I created a thread with it in the forums. I put it as a regular 2D image and childed it to the ship but moved it like 30 Z units away from it.

Like Michael says, if your camera is slightly above your ship it will be kinda confusing for aiming. My “solution” that kinda works but still isn’t that optimal was to attach a script to the crosshair that raycasts forward in a straight line and when it detects an enemy it turns red to signal the player that enemies are on range. I’m sure there are many different ways t achieve this, though. Find one that works for you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms