• 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 robert_mathew · Jun 25, 2011 at 12:29 AM · aimove

creature movement

In my game when player approaches the creature hole(rat,lizard,snake) in the terrain i want the creature to come out of the hole and move towards the player.

I have created hole in my terrain and animation for the creature movement. I want the creature to move towards the player when he approaches near the hole when creature is hiding.How to move creature towards the player when ever he is near to the hole.

thanks in advance

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xortrox · Jun 25, 2011 at 12:55 AM

creature script(name it CreatureScript):

private var Radians:float = Mathf.PI/180;

private var Angle:float = 0.0f;

private var MoveToPlayer:boolean = false;

var c_Speed:float = 5.0f;

var c_Target:GameObject;

//this is the Hole gameobject you will be asked to make longer down assign it in the

//editor:

var c_Hole:GameObject;

function Update() {

 var c_Velocity = new Vector3(0,0,0);

 if(MoveToPlayer)
 {

     transform.LookAt(c_Target);
     transform.eulerAngles.x = 0;//rotate only Y towards the targeted player
     transform.eulerAngles.z = 0;

     Angle = transform.eulerAngles.y;
     /* 
     get the creatures rotation around y, if you modelled the creature diffrently you
     might need to change this by doing +someangle at the end
     */
     c_Velocity.x = c_Speed * (Mathf.Cos(Angle*Radians))
     c_Velocity.y = rigidbody.velocity.y;
     c_Velocity.z = c_Speed * (Mathf.Sin(Angle*Radians))

     rigidbody.velocity = c_Velocity;

 }
 else
 {

     //if you want the creature to run back:
     if(c_Target != c_Hole)
     {

         c_Target = c_hole;

     }
 
     //same as before just now the target is the c_Hole object.
 
     transform.LookAt(c_Target);
     transform.eulerAngles.x = 0;
     transform.eulerAngles.z = 0;

     Angle = transform.eulerAngles.y;

     c_Velocity.x = c_Speed * (Mathf.Cos(Angle*Radians))
     c_Velocity.y = rigidbody.velocity.y;
     c_Velocity.z = c_Speed * (Mathf.Sin(Angle*Radians))

     rigidbody.velocity = c_Velocity;
     


 }

}

Now for the Cave/Hole script, you make an empty game object and place it at the location that you want the spotting range to be to that game object you add a sphere collider, and adjust the radius to what you would like the range to be.

in that game objects script you add this code:

var Creature:GameObject;

function OnTriggerEnter(iCollider:Collider) {

 //  this is the creaturescript above:
 var c_Script = Creature.GetComponent(CreatureScript);
 c_Script.c_Target = iCollider.gameObject;
 c_Script.MoveToPlayer = true;

}

function OnTriggerExit(iCollider:Collider) {

 var c_Script = Creature.GetComponent(CreatureScript);
 c_Script.MoveToPlayer = false;

}

if it doesn't work message me back :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to move to random position? 1 Answer

Moving AI away or towards player not working correctly. 1 Answer

How To Make Enemy Move Around 0 Answers

How to make an AI that moves around 1 Answer

how to combine my move script and my shoot script into 1 script 3 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