I don’t like posting a whole script, I know it is annoying but so is this problem. I have fired a raycast between two objects and successfully have found the coordinates for certain points on the first raycast. Then I try to fire another raycast from another object to the point that I have selected from the first raycast. This is what doesn’t work. I use the same logic in the second raycast as the first but it just doesn’t work.
Thanks in advanced!
using UnityEngine;
using System.Collections;
public class HorseAI : MonoBehaviour
{
Transform player;
public Transform center;
public Transform cube;
public Transform cube2;
public float speed;
public int resolution;
public int interval = 1;
Vector3 waypoint;
Vector3 point;
void Start()
{
player = GameObject.Find ("Player").transform;
}
void Update()
{
Vector3 direction = player.position - transform.position;
Vector3 direction1 = new Vector3();
RaycastHit hit;
RaycastHit cHit;
if(Physics.Raycast(transform.position, (direction), out hit,100))
{
point = (transform.position + (direction.normalized * (interval*(hit.distance/resolution))));
cube2.position = point;
Debug.DrawRay(transform.position,(direction),Color.cyan);
direction1 = cube2.position - center.position;
}
if(Physics.Raycast(center.position,(direction1),out cHit,100))
{
cube.position = (center.position + (direction1).normalized * cHit.distance);
print (waypoint);
Vector3.MoveTowards(transform.position,waypoint,speed);
if(transform.position == waypoint)
{
interval++;
}
Debug.DrawRay(center.position,direction1,Color.red);
}
}
}