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 29, 2011 at 07:50 PM · airaycasting

Raycasting Help

The problem i have is the AI should look at the player then when it hits a object with the tag Prop stop looking at the player and rotate then wait and look at the player again. This is my first attamp at AI and Raycasting so i am in a bit over my head i understand the logic of it but cant seem to get it coded correctly. I hope someone can help with this and if you do thanks :)

here is the code

 var target : Transform;
 var robotController : CharacterController;
 var speed : float = 20.5;
 var backwardsSpeed : float =-20.5;
 var rayLength : float;
 var lookAtPlayerTime : float;
 function Start()
 {
     target = GameObject.Find("MadDoc").GetComponent(Transform);
     robotController = GetComponent("CharacterController");
 }
 
 function Update()
 {
 
     
     var dir = transform.TransformDirection(Vector3.forward);
     var dirleft = transform.TransformDirection(Vector3.left);
     var dirright = transform.TransformDirection(Vector3.right);
     var hit : RaycastHit;
     Debug.DrawRay(transform.position, dir * rayLength, Color.black);
     Debug.DrawRay(transform.position, dirleft * rayLength, Color.black);
     Debug.DrawRay(transform.position, dirleft * rayLength, Color.black);
 
     if(!Physics.Raycast(transform.position, dir, hit, rayLength))
        {
                if(hit.collider.gameObject.tag == "Prop")
                {
                    transform.Rotate(0,30 * Time.deltaTime,0);
                    var forward1 : Vector3 = transform.TransformDirection(Vector3.forward);
                 var curSpeed1 : float = speed * Time.deltaTime;
                 robotController.SimpleMove(forward1 * curSpeed1);
                }
                
        }
        else 
        {    
                var forward : Vector3 = transform.TransformDirection(Vector3.forward);
             var curSpeed : float = speed * Time.deltaTime;
             robotController.SimpleMove(forward * curSpeed);    
             LookAtPlayer();
        }
        
      
 
          
 }
 function LookAtPlayer()
 {    
     yield WaitForSeconds(lookAtPlayerTime);
     transform.LookAt(target);
 }
Comment
Add comment · Show 5
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 john-essy · Nov 30, 2011 at 09:27 AM 0
Share

Please someone help!

avatar image syclamoth · Nov 30, 2011 at 09:46 AM 0
Share

What is it doing now? What should it be doing? I'm not quite sure what parts of this script you are having problems with.

avatar image john-essy · Nov 30, 2011 at 11:07 AM 0
Share

Basically i want him to look at me until he hits something. Then stop looking at me and rotate and find his way round things to get to me. If that makes any sense

avatar image john-essy · Nov 30, 2011 at 11:09 AM 0
Share

Better explanation. He should look at me until he hits something called Prop then if he does stop looking at me Rotate until he no longer hits anything and then smoothly look at me. Again if his raycast hits something then stop look at me then rotate. This is all i need him to do and he does but it is the look at player i need a way to smoothly look at me ins$$anonymous$$d of just instantly turning and looking my way.

avatar image john-essy · Nov 30, 2011 at 06:27 PM 0
Share

Anyone? .

2 Replies

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

Answer by gfr · Nov 30, 2011 at 06:50 PM

From your comment your problem is how to smoothly look at a target. For that you can gradually update the rotation every frame, e.g. using `Quaternion.Slerp`:

 // do this every update/iteration while in the "look at target" state:
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 
                                      Time.deltaTime * speed);


Comment
Add comment · Show 1 · 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 john-essy · Dec 02, 2011 at 09:57 PM 0
Share

Thanks Worked awesome

avatar image
0

Answer by john-essy · Dec 01, 2011 at 08:12 AM

Thankyou gfr i will try this right now thanks mate very much

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding RayCastHit's Origin Position 2 Answers

My navmesh agent isn't updating correctly? 0 Answers

Diagonal Raycasting to detect platform returns true, even when false 1 Answer

Raycasting and enemies ai 2 Answers

AI Raycast problem 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