Raycast always going to player

I need to write a script that has a Raycast that always goes right to the player no matter where that player is. I just can’t figure out how to get the proper Vector3 for the direction the player is in. Any ideas?

Here’s the code in C#. You need to take the player’s position and subtract the position of the game object you are raycasting from to get the direction that the player is in. It’s just like thinking about two points on a 2-dimensional graph. To get the Vector (line) between the two (rise over run), you take the second point and subtract it from the first. It’s the same thing with three dimensions.

private Vector3 direction;
private GameObject goPlayer;
private Transform transPlayer;
private Transform myTransform;

void Start()
     myTransform = this.transform;
     goPlayer = GameObject.Find("NAME_OF_PLAYER_OBJECT_HERE");
     transPlayer = goPlayer.transform;

void PlayerRaycast()
     direction = transPlayer.position - myTransform.position;
     // Do the raycast

First of all you sould find the player in the scene;
Then in Update calculate direction to the player:

Vector3 dir = (player.position - transform.position).

Now all you need is to send raycast from transform position in this direction:

Debug.DrawRay(transform.position, dir, Color.blue);

Yep good question.

So here is what you will do. Or at least this is how I would handle it.

// Where we want to go - assumes you are in a script directly associated with the person/object that always wants to know where player is.
Quaternion rotation = Quaternion.LookRotation(PlayerPosition - this.transform.position);

//If you wanted to slowly turn towards the player this is what you would do.
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);

How you use this is going to be up to you, but this is at least where I would start from. It will at least always give you the direction and rotation you need to apply to yourself to get the players position.