Checking for player facing object with raycast not working

I’m trying to check whether the player is facing towards an object that they’re colliding with. I know that they player is colliding with the object itself but the raycast that Im sending seems to be acting peculiarly.

if((movement.collisionFlags & CollisionFlags.Sides) != 0)
			{
				Vector3 start = transform.position;
				Vector3 temp = transform.position;
				float opp = Mathf.Sin(transform.eulerAngles.y * Mathf.Deg2Rad) *2;
				float adj = Mathf.Cos(transform.eulerAngles.y * Mathf.Deg2Rad) *2;
				temp.x += opp;
				temp.z += adj;
				RaycastHit hit = new RaycastHit();
				if(Physics.Raycast(start, temp, out hit, 1.0f))
				{
					Debug.Log (hit.collider);	
					
				}
				Debug.DrawLine (start, temp, Color.green, 15.0f, true);
				GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Sphere);
				cube.transform.position = temp;
			}

The primitive appears in the correct spot and the line intersects with the object but the raycast still fails. Except on one side where regardless of the direction the player is facing the raycast still succeeds. Again the primitive is in the correct spot and the drawline will avoid the object but it still succeeds regardless. There must be something Im missing but I cant see it.

Raycast() takes a position and a direction. You are passing two directions. That is your Raycast could be:

  if(Physics.Raycast(start, temp - start, out hit, 1.0f))

…or:

    if(Physics.Raycast(start, new Vector3(opp,0.0f,adj), out hit, 1.0f))

Note calculating the forward vector by using eulerAngles and sin/cos is dangerous for arbitrary rotations, and going the long way around. ‘transform.forward’ is a forward vector of the object in world space. So you can do your Raycast():

if(Physics.Raycast(transform.position, transform.forward, out hit, 1.0f))

And you can set the position of your cube by:

 cube.transform.position = transform.position + transform.forward * dist;

…where ‘dist’ is the distance in front and is 1.0 for your current code.