Maybe using Vector3.Dot ? I don’t want to check if the transform(player) is behind or in front of an object but to check if it’s facing the object.
private void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
player.GetComponent<Rigidbody>().isKinematic = false;
}
if (closeCarte == false)
{
var heading = transform.position - player.transform.position;
var dot = Vector3.Dot(heading, -transform.forward);
if (dot > 1)
{
var lookat = player.transform.position - securityKeyPadInteractable.transform.position;
lookat.y = 0;
if (lookat.magnitude <= interactableItem.distance && interactableItem != null)
{
if (!playAnimOnce)
{
if (ikControl.handFinishedMove == true)
{
if (securityKeyPad1 != null)
{
securityKeyPad1.SetActive(true);
}
if (virtualCam != null)
virtualCam.enabled = true;
freeLookCam2.enabled = false;
Cursor.visible = true;
camerasContorl.enabled = false;
player.GetComponent<Rigidbody>().isKinematic = true;
In the Update() in the top I’m checking if the user pressed S key change the Is Kinematic to false but in this part I also want to check that if the player is not facing the object securityKeyPadInteractable than also change the Is Kinematic to false.
I think it’s working the problem is that the player is not always facing the same angle to the other object.
So I can’t decide what angle to check against.
The angle checking is for the movement keys A and D.
The key S is working fine but A and D not.
When I press on A or D depending on the current player angle to the other object it will set the Is Kinematic to false too.
In this screenshot the player is facing the object and the angle is about 78 :
But if I move the player and he interact with the security object from another side the angle is not the same :
Now the angle is about 75 so it’s setting the Is Kinematic to false even if I’m not pressing any of the keys A or D or S.
So I have two problems here :
I need to check also if the keys A or D pressed and if one of them pressed to check the angle and than to decide if to set the Is Kinematic to false.
What the angle range should be ?
What am I trying to do ? I’m trying to prevent from the player to slide on the terrain slope at this point in the game for this I set the Is Kinematic to true but I also want to be able to move the player away from the security object.
The idea is not the lock the player but to make him not sliding/moving on the terrain and also to allowed to move away from it using the WSAD keys. This keys are my original moving keys using unity Third Person User Control and Third Person Character.
I’m confused how you’re getting a 78º in the first shot. Is the player position on the ground (typical), and maybe the keypad position is at the center of the face of the 2550 cube, or maybe at the center of the 2550 cube? You should consider two vectors at the same altitude, if that’s the case.