using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class InteractableItem : MonoBehaviour
{
public string currentMode;
public enum InteractableMode
{
Description,
Action,
ActionWithoutThrow
};
public InteractableMode interactableMode = InteractableMode.Description;
public float distance;
(TextArea(1, 10))
public string description = "";
public bool IsAnyAction()
{
return interactableMode == InteractableMode.ActionWithoutThrow || interactableMode == InteractableMode.Action;
}
public bool IsActionWithoutThrow()
{
return interactableMode == InteractableMode.ActionWithoutThrow;
}
private void Start()
{
currentMode = GetComponent<InteractableItem>().interactableMode.ToString();
}
private void Update()
{
}
}
/*
var heading = transform.position - player.transform.position;
var dot = Vector3.Dot(heading, -transform.forward);
if (dot > 1)
*/
The idea I think is to use Vector3.Dot at the bottom of the script I have an example of using it in another script but I want to add another enum or using the current enum in the script with option for interact with the item/s only if the player is looking at the item from the front.
Maybe to include also some angle/s limits for the front view but the idea is that I will be able to set option for each item if to look only from the front or from any side 360 degrees around.
This is a line from another script for example how I check for interaction with item :
if (RightHandToTarget && primaryTarget.IsAnyAction())
primaryTarget is InteractableItem type.
so to add and make something like :
if (RightHandToTarget && primaryTarget.IsAnyAction() && primaryTarget.LookFromFrontView)
This is the main idea logic.
and this is the full script that is attached to the player that use the InteractableItem script for interactable with items it’s a bit long so I put it in pastebin.com :
https://pastebin.com/meEuzYRx
I tried this so far since the InteractableItem script is attached to each object I want to be interactable item I added some stuff :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class InteractableItem : MonoBehaviour
{
public string currentMode;
private Transform player;
private void Awake()
{
player = GameObject.Find("Player").transform;
}
public enum InteractableMode
{
Description,
Action,
ActionWithoutThrow
};
public enum ViewMode
{
FrontView,
AllView
};
public ViewMode viewMode = ViewMode.AllView;
public InteractableMode interactableMode = InteractableMode.Description;
public float distance;
(TextArea(1, 10))
public string description = "";
public bool IsAnyAction()
{
return interactableMode == InteractableMode.ActionWithoutThrow || interactableMode == InteractableMode.Action;
}
public bool IsActionWithoutThrow()
{
return interactableMode == InteractableMode.ActionWithoutThrow;
}
private void Start()
{
currentMode = GetComponent<InteractableItem>().interactableMode.ToString();
}
private void Update()
{
var heading = transform.position - player.transform.position;
var dot = Vector3.Dot(heading, -transform.forward);
if (viewMode == ViewMode.FrontView && dot > 1)
{
Debug.Log("Front View");
}
else
{
Debug.Log("Any View");
}
}
}
First I find the Player then at the bottom I find if the player is looking from the front or from the other sides on the item and then added new enum for view mode selection. If it’s FrontView or AllView.
The problem now is how to use the first enum conditions with the second enum conditions in the Update ? The FrontView or AllView is working with the Vector3.Dot but now I need to use both enums and not sure how.
This is working but still not perfect still need to change/add things :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class InteractableItem : MonoBehaviour
{
public string currentMode;
private Transform player;
private bool frontView = false;
private InteractableMode currentInteractableMode;
private void Awake()
{
player = GameObject.Find("Player").transform;
}
public enum InteractableMode
{
Description,
Action,
ActionWithoutThrow,
NoAction
};
public enum ViewMode
{
FrontView,
AllView
};
public ViewMode viewMode = ViewMode.AllView;
public InteractableMode interactableMode = InteractableMode.Description;
public float distance;
(TextArea(1, 10))
public string description = "";
public bool IsAnyAction()
{
return interactableMode == InteractableMode.ActionWithoutThrow || interactableMode == InteractableMode.Action;
}
public bool IsActionWithoutThrow()
{
return interactableMode == InteractableMode.ActionWithoutThrow;
}
private void Start()
{
currentMode = GetComponent<InteractableItem>().interactableMode.ToString();
currentInteractableMode = GetComponent<InteractableItem>().interactableMode;
}
private void Update()
{
var heading = transform.position - player.transform.position;
var dot = Vector3.Dot(heading, -transform.forward);
if((interactableMode == InteractableMode.ActionWithoutThrow ||
interactableMode == InteractableMode.Action ||
interactableMode == InteractableMode.Description) && viewMode == ViewMode.FrontView
&& dot < 1)
{
interactableMode = InteractableMode.NoAction;
}
if (interactableMode == InteractableMode.NoAction && viewMode == ViewMode.FrontView
&& dot > 1)
{
interactableMode = currentInteractableMode;
}
}
}