- Home /
Firing a RayCast in a Raycast, Rayception
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);
}
}
}
Besides the fact that the second raycast should be placed inside the first raycast block and not out of it, but I don't understand the code in its block since you're using $$anonymous$$oveTowards with "waypoint" but you never assign this variable before and so the system is assu$$anonymous$$g it as the world origin (0,0,0)
Ok, here is an updated code, still doesn't work tho. What isn't happening is that waypoint and cube.position is not updating.
using UnityEngine; using System.Collections;
public class HorseAI : $$anonymous$$onoBehaviour
{
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;
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;
RaycastHit cHit;
if(Physics.Raycast(center.position,(direction1),out cHit,100))
{
cube.position = (center.position + (direction1).normalized * cHit.distance);
waypoint = cube.position;
print (waypoint);
//Vector3.$$anonymous$$oveTowards(transform.position,waypoint,speed);
if(transform.position == waypoint)
{
interval++;
}
Debug.DrawRay(center.position,direction1,Color.red);
}
}
}
}
Is the waypoint actually printed on your console? And do you actually see this ray?
Debug.DrawRay(center.position,direction1,Color.red);
Because if you don't then your ray simply doesn't hit anything and does not execute the code in the if... (to check where your ray is going, simply move the Debug.DrawRay to before the second if, and maybe change it to this to make it actually represent the ray you are casting)
Debug.DrawRay(center.position, direction1 * 100, Color.red);
Answer by unimechanic · Nov 13, 2014 at 04:31 PM
You are doing a physic operation on a static collider that is being moved. Use a dynamic collider instead. Check out this answer:
http://answers.unity3d.com/questions/804064/interpolation-with-jumping-acting-weird.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[Solved] Find the position ALONG a raycast 1 Answer
Rays and tags help? 1 Answer
Raycasting, LineRenderer & Layermasks 0 Answers
AI Pathfinding using RayCast 0 Answers