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 john-essy · Nov 23, 2011 at 09:05 PM · airaycasting

Rotate with Raycast???

how can i get this to rotate when the raycast is no longer hitting the collider. This is for AI i want it to rotate when it hits the collider then move when it is no longer colliding been on with this for about 7 hours someone please help:(

 function Update()
 
 {    
 
 if(canMove == true)
     {
         if(distance <= rangeToAttack)
     
         {
             var forward : Vector3 = transform.TransformDirection(Vector3.forward);
             var curSpeed : float = speed * Time.deltaTime;
             robotController.SimpleMove(forward * curSpeed);
             animation.CrossFade("walk");
             transform.LookAt(player);
             transform.eulerAngles.x = 0;
                 if(distance < 15)
                 {
                     animation.CrossFade("attack");
                     animation["attack"].speed = 0.3f;
                 }
                 
         }
     }
     
     var dir = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     Debug.DrawRay(transform.position, dir * rayLength, Color.blue);
     
     if (Physics.Raycast(transform.position, dir, hit, rayLength))
         {
             
             if(hit.collider.gameObject.tag == "Prop")
                 {
                     canMove = false;
                     transform.Rotate(0,rotateAmount,0 * Time.smoothDeltaTime);
                 }
                 else if(hit.collider = false)
                 {
                     canMove = true;
                 }
                 
             
         }
 
     
     
     
     
 }
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 Owen-Reynolds · Nov 24, 2011 at 12:27 AM 0
Share

0 * Time.smoothDeltaTime isn't doing anyone any good. Otherwise, is the problem that it should continue to rotate for a little after getting clear?

avatar image john-essy · Nov 24, 2011 at 08:18 AM 0
Share

yea it should rotate until the raycast is not hitting anything anymore

2 Replies

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

Answer by monkeyThunk · Nov 24, 2011 at 09:37 PM

 if (Physics.Raycast(transform.position, dir, hit, rayLength))

Raycast returns true if it hit something, false if it doesn't

       else if(hit.collider = false)

this line makes no sense, hit.collider is the collider that the Raycast hit, you use it above correctly (hit.collider.gameObject.tag == "Prop") but here you are actually trying to assign false to it, I think this will always evaluate to false, and you'll never set canMove to true.

I think this is what you want:

if (Physics.Raycast(transform.position, dir, hit, rayLength)) { // rotate } else { // move and maybe attack } if (hit) rotate else move and maybe attack

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 aldonaletto · Nov 24, 2011 at 10:07 AM

There are some errors in the rotation part: as @OwenReynolds said, the 3rd parameter makes no sense - you should actually multiply the 2nd parameter by Time.deltaTime to make the AI rotate smoothly; another error: the instruction if (hit.collider = false) is wrong - "=" is the assignment operator, and collider isn't boolean anyway; you should use if (!hit.collider) or if (hit.collider==null) to do something when the collider doesn't exist.
That's the fixed code:

...
if (Physics.Raycast(transform.position, dir, hit, rayLength)){
    if (hit.gameObject.tag == "Prop"){
         canMove = false;
         transform.Rotate(0,rotateAmount*Time.deltaTime,0);
    }
    else if (!hit.collider){
         canMove = true;
    }
}
NOTE: This is a syntax fix; I didn't check the logic.
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.

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

Raycasting crashing Unity? 3 Answers

Modifying AI script to follow gameobject 1 Answer

Linecast is always blocked. 2 Answers

Please Someone help with my Raycasting Rotate 0 Answers

Checking name from hit from Raycast in array 2 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