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!