- Home /
Debug.DrawRay Issue?
Hi All. I have been working on a simple AI system that randomly chooses a waypoint and goes there. So far it has been working fine. However, since it is randomly choosing a waypoint, I am adding a basic obstacle avoidance system based off of [this post][1]. So far it has been working fine, and it the raycast is detecting objects in front of the character, but when I try draw that ray, it draws the characters old path, instead of following the actual ray's path.
Here is my code
var waypoint : Transform[];
var speed : float = 50;
private var currentWaypoint : int = 0;
var loop : boolean = true;
var player : Transform;
var dist : float = 3;
private var character : CharacterController;
function Start ()
{
character = GetComponent(CharacterController);
}
function Update ()
{
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 1)
{
transform.position = target; // force character to waypoint position
currentWaypoint = Random.Range(0, waypoint.Length);
}
else
{
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10) == false) {
print ("There is something nothing in front of the object!");
Debug.DrawRay(this.transform.position, fwd, Color.green, 10);
}
else if (Physics.Raycast (transform.position, fwd, 10) == true) {
print ("There is something in front of the object!");
Debug.DrawRay(this.transform.position, fwd, Color.red, 10);
}
transform.LookAt(target);
character.Move(moveDirection.normalized * speed * Time.deltaTime);
}
}
Any and all help would be greatly appreciated. Thanks in advance! [1]: http://answers.unity3d.com/questions/235017/enemy-ai-avoiding-collision.html
var fwd = transform.TransformDirection (Vector3.forward);
$$anonymous$$aybe this is the reason. It always return a vector forward in World Space, not a vector forward of your character(local). You need to change to something like that: var fwd = transform.forward; Its just my thinking :)
No, the whole point of TransformDirection is to transform world space to local space. transform.forward
is identical to transform.TransformDirection (Vector3.forward)
. (But a lot shorter and simpler to type.)
Your answer
Follow this Question
Related Questions
Extracting RGB component data from a textured plane positioned below a 3D terrain 3 Answers
Car AI help 0 Answers
Find number of objects between two points 1 Answer
Multiple targeting help 1 Answer