- Home /
AI script problems, interaction with grenade not working, need help.
Ok so basicly these are two of many functions that make up the AI script. Essentially these two functions are designed to get the player to: A) Locate the grenade B) Turn towards it briefly C) Then turn 180 degrees and run off avoiding the grenades explosion.
The idea is to simulate a 'Holy shit, it's a bloody grenade" moment. Now the second function 'FindClosestGrenade' work fine, however what you see in AvoidGrenade is not even close to working. How in hell do i write this function ? Essentially i wanna how to do the turn towards the grenade that function FindClosestGrenade finds and then run away. The invoking of this behaviour and all that i can do fine. Please help :)
function AvoidGrenade(){ print("Message Received"); var TargetGrenade = FindClosestGrenade(); var grenadedirection = TargetGrenade.transform.position - transform.position; var from = transform; var to = TargetGrenade; var TurnRate = 1; print(grenadedirection); SendMessage("SetSpeed", 0.0); transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(grenadedirection), TurnRate * Time.deltaTime); //transform.rotation = Quaternion.Slerp(from.transform.rotation, to.transform.rotation, Time.time * TurnRate); transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
yield new WaitForSeconds (2.5);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(grenadedirection), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, grenadedirection.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
direction = forward * speed * speedModifier;
GetComponent (CharacterController).SimpleMove(direction);
SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
} function FindClosestGrenade() : GameObject { var ExistingGrenadeArray : GameObject[]; ExistingGrenadeArray = GameObject.FindGameObjectsWithTag("ActiveGrenade"); var ClosestGrenade : GameObject; var Distance = Mathf.Infinity; var position = transform.position; for (var ExistingGrenade : GameObject in ExistingGrenadeArray) { var Difference = (ExistingGrenade.transform.position - position); var curDistance = Difference.sqrMagnitude; if (curDistance < Distance) { ClosestGrenade = ExistingGrenade; Distance = curDistance; } } return ClosestGrenade; }
Thanks in Advance,
Cameron Leafe
Answer by Ricardo · Mar 07, 2010 at 04:02 PM
You mention you want to:
A) Locate the grenade B) Turn towards it briefly C) Then turn 180 degrees and run off avoiding the grenades explosion.
And state:
what you see in AvoidGrenade is not even close to working
However, you do not describe by what's happening that makes it "not even close to working". Is it that the effect is unrealistic? Is it crashing? Is it going bonkers?
As to how to write it, there are many possible approaches. One would be:
- Get a vector from the grenade towards your character
- Extend a vector from your character's position for whatever the character feels is a safe distance
- Flee towards that vector at an increasing speed (as he will need to accelerate)
I'm not sure why you'd turn 180 degrees, unless your character's whole body is turning towards the grenade - bear in mind that a human being would likely just turn their head, unless the grenade they perceived fell more than 90 degrees from their current heading. This means that in order to flee it, it would be unnecessary and inefficient to turn 180 degrees from their current orientation.
well i was thinking he would turn and face the grenade, as in his whole body would. And then from that heading he would turn 180 degrees and run away. Essentially i have pretty limited program$$anonymous$$g experience so this sort of stuff is like nailing jelly to a wall. What happens atm when i activate this function with a grenade near bye he stops very briefly turns, but not always towards it and then spaz's out for several seconds and then goes back to what he was doing.
Your answer
Follow this Question
Related Questions
free roaming enemy ai 1 Answer
Can you bake nav mesh in prefab mode? 0 Answers
Objects jump when game is started 0 Answers
Move NavMeshAgent at a constant speed 1 Answer
Spawning enemy at random spawn points without Overlapping! 2 Answers