Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cameron · Mar 07, 2010 at 01:37 PM · ai

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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:

  1. Get a vector from the grenade towards your character
  2. Extend a vector from your character's position for whatever the character feels is a safe distance
  3. 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cameron · Mar 08, 2010 at 03:54 AM 0
Share

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.

avatar image
0

Answer by kmlt · Apr 05, 2010 at 03:11 PM

Try using transform.LookAt(Target)!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges