• 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 wolga2 · Jul 17, 2012 at 03:18 AM · avoid

Make an Object avoid another

hi! how can i make an object1 avoid object2? so if the object2 comes towards object1, it will move away from it?

thank you very much!

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
Best Answer

Answer by twoface262 · Jul 17, 2012 at 04:11 AM

 var hit : RaycastHit;
 var forward  = transform.TransformDirection(Vector3.forward);
 if(Physics.Raycast(transform.position,forward,hit, 5)){
    if(hit.collider.tag == "objecttag"){
       transform.Rotate(Vector3.up * 10);
 }}


What that does is point a raycast in front of your AI. If the ray hits the tag of your objecta/objectb it'll rotate the object to avoid the other object. All you have to do to this code is change the "objecttag" to the tag of your object. Enjoy!

Comment
Add comment · Show 2 · 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 wolga2 · Jul 17, 2012 at 01:40 PM 0
Share

thanks a lot!! :) how could i write the code, that the object even avoids the other, when its behind or next to it? with Physics.SphereCast?

avatar image twoface262 · Jul 17, 2012 at 04:02 PM 0
Share

It would be similar to that. Just use this - http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AI to make an object wander around and avoid obstacles? 5 Answers

Simple shader question: let light affect all faces (remove occlusion?) 0 Answers

What's the easiest way to create an Instance Manager as to avoid calling Instantiate during game runtime? 1 Answer

Shoot him up with a ball 1 Answer

Can Navmeshagent avoid other agent? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges