Rotation a Physics.Raycast problem...

Hi,

I am working on a system and am trying to achieve something, but my coding experience isn’t high enough, so I’m thinking that I am getting confused by something basic here. Here’s the deal…

I want to make a system that if TRUE is returned it does a 180 degrees ray sweep in a FOR loop, each time iterating by 5 degrees and instantiating a prefab at the ray.hit.point vector…

I have tried something like this:

var Target : Transform;
var Waypoint : GameObject;

function Update () 
{
	var hit : RaycastHit;
	
	if(Physics.Raycast(Target.position, Target.forward, hit))
	{		
		var ray = new Ray (Target.position, Target.forward);
		
		for(i = 0; i <= 30; i ++)
		{
			if(Physics.Raycast(ray, hit))
				Point = hit.point;
				
			print (Point);
			
			Instantiate(Waypoint, Point, Quaternion.identity);
			
			Target.rotation.y += 5.0;
		}
	}
}

But this of course doesn’t work; first of all, I am getting TRUE back all the time so I get 30 spheres per frame instantiated which isn’t the point at all, and secondly, there’s no turning happening at all… I know I am going about this the wrong way, but I have tried so many different things and now I’m at the end of ways I can turn this thing around and look from another direction, so any help, advice or nudge in the right direction will be greatly appreciated :slight_smile:

Many thanks in advance :slight_smile:

First… Target.rotation.y… needs to be Target.localEulerAngles.y That will do the rotation you need.

Second… If the waypoints have a collider and they are close enough, they will spawn them all on the target, or near enough to the target that it would defeat the purpose.

Third… Assuming from what you have, you always turn Left?

Fourth… Nothing here denotes a distance. So a wall 10 units away is the same as a wall 1000. If not, you may wish to put in a length value in the raycast

Fifth… Help us to understand the overall problem. This seems to be a great pain to go through for an AI pathfinding.

Hi,

Thanks for your reply :slight_smile: Yes, you’re right that approach was terribly flawed, as I mentioned I am very new to coding and I wanted to try out a different approach to pathfinding. I find it a good way to learn new things when experimenting and trying to figure stuff on your own, rather than just extensive use of Copy and Paste :slight_smile:

Anyway, I have abandoned that approach and went for a more traditional A* method, but I am still quite interested in working out a pathfinding system with raycasting, it would be an interesting challenge, so any additional ideas on how I should approach that problem are welcome :slight_smile: All the help I could find online regarding pathfinding is talking about A* method, no one mentions raycast way, but I have seen systems that work very nicely, so I’d like to learn how to do that :slight_smile: