Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Somiaz · Apr 01, 2021 at 06:33 PM · cameraremovetargetinggroupmember

Target Group will not RemoveMember or Lock On Properly

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Somiaz · Apr 02, 2021 at 05:03 PM

Someone has figured this out on a different platform. They said to put line 136 below RemoveMember, this worked. I also deleted FindMember as it was useless in this case and was not needed.

They weren't sure about the distance the player needed to be, so I changed line 152 from float distance = lockOnDistance * 2f; to float distance = Mathf.Infinity;. This has fixed the distance issue.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How would one go about creating a Targeting System similar to the z-targeting in LoZ? 2 Answers

Camera re-target 2 Answers

Drawing GUI rectangle uses negative y coordinate 1 Answer

I need to remove the GUI from this camera script, but I don't know how. 2 Answers

How to save position of camera 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges