I’m doe with this lecture, However when I click on the tab again more and more cameras get added not removed (But only one) any suggestions?
I’m not seeing more cameras in the scene, but I do see several spheres on the CinemachineTargetGroup. I’m assuming this is what you’re referring to.
Every time you press the + button, an additional target will be added, but since Unity doesn’t know what that target is, it copies the last entry…
You can delete these by selecting them and pressing the “-” button.
That is correct. The adding and removing of these cameras is from code. I’m just wondering where or if I missed anything for removing the ones added.
You’re adding cameras from code? Or targets?
Paste in the code that is adding Cameras, since that’s not something that’s supposed to be happening.
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;
public class Targeter : MonoBehaviour
{
//private float targetWeight = 0.25f;
//private float targetRadius = 2f;
[SerializeField] private CinemachineTargetGroup cineaTargetGroup;
private Camera mainCamera;
public List<Target> targets = new List<Target>();
public Target CurrentTarget {get; private set;}
private void Start()
{
mainCamera = Camera.main;
}
private void OnTriggerEnter(Collider other)
{
if (!other.TryGetComponent<Target>(out Target target)) { return; }
targets.Add(target);
target.OnDestroyed += RemoveTarget;
}
private void OnTriggerExit(Collider other)
{
if (!other.TryGetComponent<Target>(out Target target)) { return; }
RemoveTarget(target);
}
public bool SelectTarget()
{
if(targets.Count == 0 ) {return false;}
Target closestTarget = null;
float closestsTargetDistance = Mathf.Infinity;
Vector2 toCenter;
foreach (Target target in targets)
{
Vector2 viewPos = mainCamera.WorldToViewportPoint(target.transform.position);
if (viewPos.x < 0 || viewPos.x > 1 || viewPos.y < 0 || viewPos.y > 1 )
{
continue;
}
toCenter = viewPos - new Vector2(0.5f ,0.5f);
if(toCenter.sqrMagnitude < closestsTargetDistance)
{
closestTarget = target;
closestsTargetDistance = toCenter.sqrMagnitude;
}
}
if(closestTarget == null) {return false;}
CurrentTarget = closestTarget;
//Adding a target to the group of targets (Enemies)
cineaTargetGroup.AddMember(CurrentTarget.transform, 1f, 2f);
return true;
}
public void Cancel()
{
if(CurrentTarget == null) {return;}
cineaTargetGroup.RemoveMember(CurrentTarget.transform);
Debug.Log(CurrentTarget + "TEST22");
CurrentTarget = null;
}
private void RemoveTarget(Target target)
{
if(CurrentTarget == target)
{
cineaTargetGroup.RemoveMember(CurrentTarget.transform);
CurrentTarget = null;
Debug.Log("Add OR Remove TEST 1");
}
target.OnDestroyed -= RemoveTarget;
targets.Remove(target);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Target : MonoBehaviour
{
public event Action<Target> OnDestroyed;
private void OnDestroy()
{
Debug.Log("OndestryFunction");
OnDestroyed?.Invoke(this);
}
}
So if the issue is that the target is getting added multiple times… perhaps forcing a removal in SelectTarget is a solution here:
Try adding this to the beginning of SelectTarget()
if(CurrentTarget!=null) cineaTargetGroup.Remove(CurrentTarget.transform);
I get an error:
There is no argument given that corresponds to the required formal parameter ‘value’ of ‘CollectionExtensions.Remove<TKey, TValue>(IDictionary<TKey, TValue>, TKey, out TValue)’ [Assembly-CSharp]csharp(CS7036)
it’s under the word “Remove”
My mistake, that was meant to be RemoveMember
, like it is in the RemoveTarget method.
I tried that too.
‘CinemachineTargetGroup’ does not contain a definition for ‘RemoveTarget’ and no accessible extension method ‘RemoveTarget’ accepting a first argument of type ‘CinemachineTargetGroup’ could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]csharp(CS1061)
RemoveMember
That worked Thank you !
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.