Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Khale06 · Sep 15, 2013 at 12:02 PM ·

Prevent a Character Model to pass through another object

I have a character model with a rigid body attached to it. How can I prevent this model passing through other objects in the scene. FYI, I'm moving the object by altering it's transform. Also the movement of the model is influenced by mouse click, somewhat like LOL or DOTA.

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

Answer by SunnySunshine · Sep 15, 2013 at 06:18 PM

You should add colliders to it.

If it's a character you're controlling, you should drop the rigid body and use the character controller: http://docs.unity3d.com/Documentation/Components/class-CharacterController.html

You can move it with collision detection using Move: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html

Something like this:

 void Move()
 {
     Vector3 dir = new Vector3(target.position.x, transform.position.y, target.position.z) - transform.position;
     float dist = dir.magnitude;        
     float move = speed * Time.deltaTime;
     
     if(dist > move)
         cc.Move(dir.normalized * move); // cc is reference to the character controller
     else
         cc.Move(dir);
 }

You can download an example package I made here:

http://speedy.sh/XNZdT/move.unitypackage

Comment
Add comment · Show 3 · 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 Khale06 · Sep 16, 2013 at 03:15 AM 0
Share

One problem still persist. I'm moving the character by clicking on the terrain (just like LOL and DOTA). I tried to add a character controller, it didn't work. Here's my code

 void $$anonymous$$oveTo() //function for moving
     {
         Vector3 dir = targetPosition - transform.position;
         float dist = dir.magnitude;        
         float move = speed * Time.deltaTime;
         if(dist > move)
         {
             transform.position += dir.normalized * move;
         } 
         else 
         {
             transform.position = targetPosition;
             anim.SetFloat(hash.moveSpeedFloat, 0f);  // sets the speed variable in the animator
         }
         //$$anonymous$$ovement
         transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
         //Rotation Smooth
         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotateSpeed);        
     }
avatar image SunnySunshine · Sep 16, 2013 at 07:34 AM 0
Share

You see, you need to use the move function. I updated the answer with code and a unity package example that you can download here http://speedy.sh/XNZdT/move.unitypackage . Just import it and run the demo scene.

If you want true dota movement, you need to make use of a path finding algorithm too. This is no trivial task if you're not used to program$$anonymous$$g. It might be a good idea to make use of pre-existing rts-controllers. I'm sure there are some in the asset store. Good luck.

avatar image Khale06 · Sep 16, 2013 at 09:52 AM 0
Share

I'm actually a C# programmer mainly for business solution softwares. I'm fairly new into game program$$anonymous$$g, but kind of getting a hold on it. Thank BTW.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Roll a ball - project 0 Answers

How to draw an arrow from fps contoller 1 Answer

getting high score of game from asp.net page 2 Answers

How to get rid of the input selection screen at beginning of game 1 Answer

3d UI Using plane For all screens 0 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