I use Cinemachine for this to work. My goal is to remove a target group member when I stop locking on to an enemy. However when I stop holding the right mouse button the remove memeber doesn’t work. I’m not sure why this is happening as before it wouldn’t remove the first member/ lock on target but would remove the others. My lockedObject Transform variable will happily reset to null when I’m not locking on to anything.
Also, it won’t lock on properly until the player character is up clost to the enemy and not within the larget radius it should be set to. I used the code from FindClosestEnemy and renamed it to GetClosestTarget as I’m wanting to allow the player to target certain objects to help them out. This code seems to work for the most part except for the distance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class PlayerMovement : MonoBehaviour
{
[Header("Unity General")]
public CharacterController controller;
public Transform cam;
[Header("General Settings")]
public float speed = 6;
public float gravity = -11f;
public float jumpHeight = 3f;
Vector3 velocity;
public bool isGrounded;
[Header("Gravity")]
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
[Header("Camera Smoothing")]
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
[Header("Lock On")]
public Animator animator;
public Transform lockedObject;
public bool isLockingOn;
public Animator effectAnimator;
[Space]
public bool canLockOn;
public float lockOnDistance;
public LayerMask lockOnMask;
public PlayerItems items;
[Space]
public CinemachineTargetGroup targetGroup;
public GameObject playerCam;
public GameObject lockCam;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
public void Update()
{
//Allows the player to jump.
//isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0)
{
velocity.y = -6f;
}
//Allows the player to walk.
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
isGrounded = false;
}
//Simulates gravity.
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
LockOnTarget();
}
void LockOnTarget()
{
canLockOn = Physics.CheckSphere(transform.position, lockOnDistance, lockOnMask);
//targetColliders = Physics.OverlapSphere(transform.position, lockOnDistance, lockOnMask);
if (canLockOn)
{
Debug.Log("Lock");
GetClosestTarget();
}
if (Input.GetMouseButton(1) && canLockOn)
{
if (!items.equipItem)
{
if (effectAnimator.GetCurrentAnimatorStateInfo(0).IsName("LockOffIdle"))
{
//animator.SetTrigger("Out");
effectAnimator.SetTrigger("On");
isLockingOn = true;
playerCam.SetActive(false);
lockCam.SetActive(true);
if (GetClosestTarget() != null)
{
lockedObject = GetClosestTarget().transform;
targetGroup.AddMember(lockedObject, 5f, 5f);
}
}
}
}
else if (Input.GetMouseButtonUp(1) || !canLockOn)
{
if (!items.equipItem)
{
if (effectAnimator.GetCurrentAnimatorStateInfo(0).IsName("LockOnIdle"))
{
//animator.SetTrigger("In");
effectAnimator.SetTrigger("Off");
isLockingOn = false;
playerCam.SetActive(true);
lockCam.SetActive(false);
if (GetClosestTarget() != null)
{
lockedObject = null;
targetGroup.FindMember(lockedObject);
targetGroup.RemoveMember(lockedObject);
}
}
}
}
}
GameObject GetClosestTarget() // Calculates what the closets target the player locks on to, then applies that transform into the extra cinemachine target.
{
GameObject[] gameObjects;
gameObjects = GameObject.FindGameObjectsWithTag("LockOn");
GameObject closest = null;
float distance = lockOnDistance * 2f;
Vector3 position = transform.position;
foreach (GameObject _gameObject in gameObjects)
{
Vector3 diff = _gameObject.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = _gameObject;
distance = curDistance;
}
}
return closest;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lockOnDistance);
}
}