Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 dd3kk · Dec 10, 2012 at 11:05 PM · wallswalkonstickingspider

How to make enemy walk on walls, while go toward player?

I need to make enemy walk on walls, like a spider, while follow the player. For example, between the enemy and the player there is a box, the enemy instead of avoid this, must walk on, sticking to the walls.

Thanks

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

6 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Dec 12, 2012 at 08:40 AM

I found this question very interesting so decided to have a go. I used this answer by Aldo as a reference : http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html

It is far from perfect, and I have only tested it in the attached scene, but see how it goes : http://www.alucardj.net16.net/unityanswers/WalkOnWalls1.unitypackage

what it does is check for 1/ a wall infront of the object and 2/ no floor below object. When either of these conditions are met, a raycast determines the normal of the next surface to walk along, rotates the object to that normal, then keeps on trucking !

Script :

 #pragma strict
 
 var myTransform : Transform;
 var speed : float = 5.0;
 var isWalking : boolean = true;
 var curNormal : Vector3 = Vector3.up;
 var hitNormal : Vector3 = Vector3.zero;
 
 function Start() 
 {
     myTransform = transform;
 }
 
 function Update() 
 {
     switch( isWalking )
     {
         case true :
             // check for wall
             var rayHit : RaycastHit;        
             if ( Physics.Raycast( myTransform.position, myTransform.forward, rayHit, 0.5 ) )
             {
                 hitNormal = rayHit.normal;            
                 isWalking = false;
             }
             Debug.DrawRay( myTransform.position, myTransform.forward * 1.0, Color.red );    // show forward ray    
             
             // check for no floor    
             var checkRear : Vector3 = myTransform.position + (-myTransform.forward * 0.25);
             if ( Physics.Raycast( checkRear, -myTransform.up, rayHit, 1.0 ) )
             {
                 // there is a floor!
             }    
             else
             {
                 // find the floor around the corner
                 var checkPos : Vector3 = myTransform.position + (myTransform.forward * 0.5) + (-myTransform.up * 0.51);
                 Debug.DrawRay( checkPos, -myTransform.forward * 1.5, Color.green );    // show floor check ray
                 if ( Physics.Raycast( checkPos, -myTransform.forward, rayHit, 1.5 ) )
                 {
                     Debug.Log( "HitNormal " + rayHit.normal );            
                     hitNormal = rayHit.normal;            
                     isWalking = false;
                 }
             }            
             Debug.DrawRay( myTransform.position, -myTransform.up * 1.0, Color.red );    // show down ray
             // move forward
             MoveForward();
         break;
         
         case false :
             curNormal = Vector3.Lerp( curNormal, hitNormal, 4.0 * Time.deltaTime );
             var grndTilt : Quaternion = Quaternion.FromToRotation( Vector3.up, curNormal );
             transform.rotation = grndTilt;            
             var check : float = (curNormal - hitNormal).sqrMagnitude;        
             if ( check < 0.001 )
             {
                 grndTilt = Quaternion.FromToRotation( Vector3.up, hitNormal );            
                 transform.rotation = grndTilt;            
                 isWalking = true;
             }
         break;
     }
 }
 
 function MoveForward() 
 {
     myTransform.position += transform.forward * speed * Time.deltaTime;
 }
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 AlucardJay · Dec 12, 2012 at 10:23 AM 0
Share

I should say incase it is not obvious, this is only half an answer. There is no targetting involved, this script just keeps walking forward. Hopefully someone can expand on this to include a target. $$anonymous$$aybe Aldo can help if he sees this =]

avatar image dd3kk · Dec 12, 2012 at 11:00 AM 0
Share

Thank you very much. I am preparing to study this script. I tried it myself first to ask the forum to do some testing with the Raycast. Of course it works, but I have problems with normal that sometimes are out of phase or with the rigidbody for collisions, which causes problems. Unfortunately I am not a professional programmer. I will try to use the script and if there are meanwhile will insert progress.

avatar image
0

Answer by dd3kk · Dec 12, 2012 at 11:07 AM

Thank you very much. I am preparing to study this script. I tried it myself first to ask the forum to do some testing with the Raycast. Of course it works, but I have problems with normal that sometimes are out of phase or with the rigidbody for collisions, which causes problems. Unfortunately I am not a professional programmer. I will try to use the script and if there are meanwhile will insert progress.

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
avatar image
0

Answer by dd3kk · Dec 15, 2012 at 01:53 PM

I finally wrote the script. I extracted from yours, the lines for the rotation. It works fine for now, including follow the player. The single biggest problem is that rotates correctly only when rotated in one direction. If the rotate towards an other face of the cube then rotating the enemy on another axis, just touches the face of the object, in a frame rotates in the wrong direction. I've tried everything. however, I insert here a few frames to explain. I hope it is clear and sorry for the bad english. thanks

this is the code I use for the rotation, but I tried other solutions and it always comes to the same result.

 curNormal = Vector3.Lerp(curNormal, hitZnormal, 6.0 * Time.deltaTime);
     var tiltToNormal : Quaternion = Quaternion.FromToRotation(Vector3.up, curNormal);
     transform.rotation = tiltToNormal;

GOOD - FRONT alt text BAD - RIGHT SIDE alt text


good2touch.jpg (18.9 kB)
bad2touch.jpg (31.0 kB)
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
avatar image
0

Answer by dd3kk · Dec 16, 2012 at 03:46 AM

Hi! Fortunately, I solved the problem by changing the "Vector3.up" in "transform.up" and adding, outside the brackets the string "* transform.rotation"

here, the code: var tiltToNormal : Quaternion = Quaternion.FromToRotation(transform.up, curNormal)*transform.rotation;

Thanks anyway!

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
avatar image
0

Answer by rubendariohh · Dec 31, 2017 at 10:43 PM

Can you please share the C# Script?

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
  • 1
  • 2
  • ›

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

12 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

Related Questions

Rigidbody Sticks to Walls 3 Answers

Walking On Walls With Mouselook! 3 Answers

Freezing Z axis makes my character stick to walls 0 Answers

Walking through walls 1 Answer

Walking on walls? 4 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