How can I raycast between two moving objects?
I want to raycast from a moving enemy to a moving player. I dont know how to actually code to make the direction work.
using UnityEngine;
using System.Collections;
public class Enemy_Manage_Two : MonoBehaviour {
public GameObject player;
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
player = GameObject.FindGameObjectWithTag ("Player");
//Debug.Log (player.transform.position + " " + transform.position);
Ray ray = new Ray (transform.position, player.transform.position);
RaycastHit hit;
Debug.DrawRay (transform.position, player.transform.position,
Color.red);
if(Physics.Raycast(ray, out hit)) {
gameObject.renderer.material.color = Color.blue;
} else {
gameObject.renderer.material.color = Color.white;
}
}
}