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
1
Question by BobbleHead · Jul 03, 2012 at 11:49 AM · aienemyspherecast

Spherecast if hit.distance <=, move the otherway

I'm writing a simple(!) script, where player is chased by an enemy. This enemy should never actually touch the player, and like a school playground game, if the player turns around or is about to collide with enemy, the enemy would run away.

However, it's not quite working, I keep getting a warning in regards to the following line:

         if(hit.distance <= 5.0){  

This is my entire update function:

 function Update () {

     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
     var layerMask = 8;   
     var hit : RaycastHit;    
     var charCtrl : CharacterController = GetComponent(CharacterController);    
     var p1 : Vector3 = transform.position + charCtrl.center;

     if (Physics.SphereCast (p1, charCtrl.height / 2, transform.forward, hit, layerMask, 10.0)) {
         distanceToObstacle = hit.distance;
         if(hit.distance <= 5.0){  
             transform.Rotate(0,25,0);
         }
     }  
 }


This my first experiment with SphereCast, so that could be the problem!

Thank you in advance!

Comment
Add comment · Show 2
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 Prodigga · Jul 03, 2012 at 12:35 PM 0
Share

what is your warning?

avatar image BobbleHead · Jul 03, 2012 at 12:57 PM 0
Share

"The referenced script on this Behaviour is missing!" Doesn't make sense =/

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Jul 03, 2012 at 12:40 PM

The last two arguments for SphereCast are in the wrong order: they should be range and layerMask. Check also if the layer mask is ok, or remove this parameter - Unity will assume all layers by default.

Comment
Add comment · Show 5 · 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 BobbleHead · Jul 03, 2012 at 12:58 PM 0
Share

thanks Aldo, didn't realise that. Still not perfect though. Seems to ignore all collisions now. Perhaps layermask isn't right, will have to double check but it was fine.

avatar image Bunny83 · Jul 03, 2012 at 01:08 PM 1
Share

Yes, this way you use layer "3", not layer "8" ( == 2^3. The Layermask is a bitmask, not a simple number that specifies a single layer. Each bit in the mask represents one layer.

Binary wise your "8" looks like this:

 00000000 00000000 00000000 00001000
                               |||||
                               ||||\- layer 0
                               |||\-- layer 1
                               ||\--- layer 2
                               |\---- layer 3
                               \----- ...

If you want the layer "8" you have to use either 2^8 == 256 or use the Layer$$anonymous$$ask type which can convert this for you.

I can't imagine any reasonable warning on this line:

 if(hit.distance <= 5.0){  

It would help to know what the warning is telling you. Are you sure it's on this line?

avatar image BobbleHead · Jul 03, 2012 at 01:58 PM 0
Share

I figured out that, the warning was unrelated and due to a prefab missing a script. odd it kept redirecting me to that line though.

However, i almost certainly haven't used layermasking properly, so am going to have to learn about it!

 var layer$$anonymous$$ask :Layer$$anonymous$$ask = 1<<8;   

seems to do the trick

avatar image BobbleHead · Jul 03, 2012 at 02:17 PM 0
Share

Oh, and this is where I really am stuck, how can I take the enemy position and then basically reverse it if the enemy is too close?

i.e add a force in the opposite direction

avatar image aldonaletto · Jul 04, 2012 at 05:15 AM 1
Share

How are you moving the enemy? With the code in the first two lines of Update? If so, revert the speed with something like this:

private var dir: float = 1;

function Update () { var moveDir = target.position - myTransform.position; var dist = moveDir.magnitude; if (dist 10) dir = 1; // restore direction if > 10 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(moveDir), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward dir moveSpeed * Time.deltaTime; ... } $$anonymous$$aybe you don't even need the SphereCast (unless you want to use it to avoid other obstacles).
NOTE: If your enemy is a CharacterController, you should use $$anonymous$$ove or Simple$$anonymous$$ove to move it:

 GetComponent(CharacterController).Simple$$anonymous$$ove(myTransform.forward * dir * moveSpeed);

You should pass the velocity vector to Simple$$anonymous$$ove (gravity is automatically applied).

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

7 People are following this question.

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

Related Questions

SphereCastAll sees for miles 2 Answers

How can I to tell if something blocks the vision of my enemy? 1 Answer

Why is this script causing a lot of lag? 2 Answers

How to make an object jump? 0 Answers

switch prefab on collision 1 Answer


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