Facebook
From diamond_collector, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 42
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DetectPlayerPosition : MonoBehaviour
  6. {
  7.     [SerializeField] Transform playerPosition;
  8.     [SerializeField] float _dotResult;
  9.     float angleBetweenObjectAndPlayer;
  10.  
  11.     // Update is called once per frame
  12.     void Update()
  13.     {
  14.         var distanceToPlayer = playerPosition.position - transform.position;
  15.         var distanceToPlayerNormalized = distanceToPlayer.normalized;
  16.         var transformRightNormalized = transform.right.normalized;
  17.         _dotResult = Vector3.Dot(distanceToPlayerNormalized, transformRightNormalized);
  18.  
  19.         if(_dotResult > 0)
  20.         {
  21.             Debug.Log("Right");
  22.         }
  23.         else if (_dotResult < 0)
  24.         {
  25.             Debug.Log("Left");
  26.         }
  27.         else
  28.         {
  29.             Debug.Log("Ahead or behind");
  30.         }
  31.     }
  32. }