2D Raycasting and interacting with 2D Objects,
I would like to know how to transform this script (Raycast Manager) to fit the 2D character. I've been looking for that so long but I still cant find the right way.. And also I want to know if there is a mistake in the Object Properties Script. My goal here is when I get closer to the object be able to press ''E'' and interact with the object.
Thanks in advance!
The RaycastManager Script:
public class RaycastManager : MonoBehaviour { private GameObject raycastedObj;
public float rayLenght = 5f;
public LayerMask newLayerMask;
public PlayerStats PlayerStats;
void Update()
{
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, forward, out hit, rayLenght, newLayerMask.value))
{
if (hit.collider.CompareTag("Consumable"))
{
raycastedObj = hit.collider.gameObject;
if(Input.GetKeyDown(KeyCode.E))
{
//Obj props.
raycastedObj.GetComponent<ItemProperties>().Interaction(PlayerStats);
}
}
}
}
}
The Object Properties Script:
public class ItemProperties : MonoBehaviour { public string itemName;
[SerializeField] private bool food;
[SerializeField] private bool water;
[SerializeField] private bool health;
[SerializeField] private float value;
[SerializeField] private PlayerStats PlayerStats;
public void Interaction(PlayerStats playerVitals)
{
if (food)
{
playerVitals.HungeBar.value += value;
this.gameObject.SetActive(false);
}
}
} ,
Comment