- Home /
Vector3.Movetoward. Why does this work?
void Update ()
{
moveDist = speed * Time.deltaTime;
Vector3 posDir = AINodePoints [curIndex].transform.position - transform.position;
Vector3 rotDir = Vector3.RotateTowards(transform.forward,posDir,moveDist,0.0f);
transform.position = Vector3.MoveTowards (transform.position, AINodePoints [curIndex].transform.position,moveDist);
transform.rotation = Quaternion.LookRotation(rotDir);
if(transform.position == AINodePoints[curIndex].transform.position)
{
curIndex = 1;
}
if(transform.position == AINodePoints[curIndex].transform.position)
{
curIndex = 0;
}
Debug.DrawRay(transform.position, rotDir, Color.red);
RaycastHit enemySight;
if (Physics.Raycast (transform.position, Vector3.back,out enemySight,500.0f))
{
string sightTag = enemySight.transform.tag;
if(sightTag == "Player")
{
transform.position = Vector3.MoveTowards (transform.position, Player.transform.position,moveDist);
}
}
}
}
But this doesn't?
void Update ()
{
moveDist = speed * Time.deltaTime;
RaycastHit enemySight;
if (Physics.Raycast (transform.position, Vector3.back,out enemySight,500.0f))
{
string sightTag = enemySight.transform.tag;
if(sightTag == "Player")
{
transform.position = Vector3.MoveTowards (transform.position, Player.transform.position,moveDist);
}
else if(sightTag == "Node")
{
Debug.Log("Running around");
Vector3 posDir = AINodePoints [curIndex].transform.position - transform.position;
Vector3 rotDir = Vector3.RotateTowards(transform.forward,posDir,moveDist,0.0f);
transform.position = Vector3.MoveTowards (transform.position, AINodePoints [curIndex].transform.position,moveDist);
transform.rotation = Quaternion.LookRotation(rotDir);
if(transform.position == AINodePoints[curIndex].transform.position)
{
curIndex = 1;
}
if(transform.position == AINodePoints[curIndex].transform.position)
{
curIndex = 0;
}
Debug.DrawRay(transform.position, rotDir, Color.red);
}
}
}
}
I do have two objects with the tag "Node" but it seems to just freeze up ins$$anonymous$$d of continuing on.
When possible, please post only the snippets which are relevant - nobody wants to proofread 120 lines for free. ;)
Please state the line or lines which you want proofread so we can easily locate your issue.
Just the Update really, these things tend to go faster the more info you get.
Alright snipped out half of it.
This is still a lot to proof without knowing more about what you're trying to achieve. A description of what is happening versus what you expect to happen is always helpful. In what way does the latter script not work? What behavior do you expect versus what behavior is observed? Can you insert some Debug.Log statements to help yourself deter$$anonymous$$e where the issue is?
The first script has the enemy object returning to two points on the list. If the player comes into sight of the Raycast, the enemy moves towards the player ins$$anonymous$$d. If the player gets out of sight, the enemy returns to patrolling.
What happens on the second one, the enemy doesn't patrol or go after the player when it comes into sight.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
FIXED: How can I stop a collider from tunneling? 2 Answers
Void Execution Question 0 Answers
Balancing Forces + Gravity 1 Answer
Finding the axis of a curving pipe 0 Answers