I don't know what I'm doing wrong, so hopefully someone will want to give some quick advice.
The player object is controlled by the keyboard (WASD, Enter/Return), and it activates objects around him when the player presses Enter. I don't want the object to activate unless the player is facing the object, so I tried to use Raycasting from the player to the collider object (now simply a cube) that surrounds the object:
//Get the ray coming from Player
public bool IsFacingObject()
{
return (Physics.Raycast(transform.position, transform.forward, 10));
}
The above method returns true if the ray coming from the player hits something, false if it doesn't.
Now for the Box collider:
void Update()
{
if (canTalk && Input.GetButtonDown("Enter") && player.GetComponent<LukaCController>().IsFacingObject())
{
if (GetComponent<MessageBoxScript>())
{
Destroy(GetComponent<MessageBoxScript>());
}else
{
begin = gameObject.AddComponent<MessageBoxScript>();
begin.MessageBox(font1, font2, faceGraphic,
0, "Convo Box", true, "Hi, I'm the Convo Box!$ How are you?");
}
}
}
canTalk is a boolean that returns true if the player's collider hits the trigger that surrounds the cube. player.GetComponent().IsFacingObject() is the method I showed you above, returning true or false.
However, player.GetComponent().IsFacingObject() doesn't return true, even though the player is facing the cube and is even touching the cube. Why isn't the raycast seeing the cube?