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 Joetjah · Jan 26, 2013 at 02:34 PM · javascriptraycasttransform

Trouble with raycasting

I'm trying to script my AI on a simple way. The AI does a raycast in front, left and right of it. Then it takes a random direction in a way that doesn't contain a "Boundary"-element.

First, my Update() checks if it's time to calculate a new direction. If it is, it calculates the new direction, then it moves to that.

I'm using the following code to move:

 Debug.DrawLine(transform.position, transform.position + transform.forward, Color.yellow);
 Debug.DrawLine(transform.position, transform.position + transform.right, Color.yellow);
 Debug.DrawLine(transform.position, transform.position - transform.right, Color.yellow);
     //DEBUGS START AND END POSITION ARE CORRECT

 var startTime;
 if (Time.time > nextUpdate) {
     Debug.Log("New check");
     
     var dirWay = MoveDirection();
     //if (dirWay == 0)
         //rot = Quaternion.Euler(0, 0, 0);
     if (dirWay == 1) {
         rot = Quaternion.Euler(0, 90, 0);
     }
     if (dirWay == 2) {
         rot = Quaternion.Euler(0, -90, 0);
     }
     if (dirWay == 3) { //backwards
         rot = Quaternion.Euler(0, 180, 0);
     }
     nextUpdate = Time.time + walkTime; //for example, 2: Every 2 seconds an update
     direction.y = 1;
     direction.y = 1.5 - transform.position.y;
     transform.rotation = transform.rotation * rot;
     transform.position = transform.position + transform.forward + transform.forward;
     //Plus 2 * transform.forward because it moves 2 places
 } 

The function MoveDirection checks for obstacles through raycasting. My AI moves the correct distance in the correct time, but walks through walls. That means my raycasting is wrong. I'm using the following code:

 var obstacles = ["Border", "Boundary", "BoundaryFlame"]; 
 var frontAvailable = true;
 var leftAvailable = true;
 var rightAvailable = true;
 var hitFront: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9)) {
     for (var i = 0; i < obstacles.length; i++)
     {
         if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) > -1)
         {
             frontAvailable = false;
         }
     }
 }
 var hitLeft: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position - transform.right, hitLeft, 1.9)) {
     for (var j = 0; j < obstacles.length; j++)
     {
         if (hitLeft.collider.gameObject.name.IndexOf(obstacles[j]) > -1)
         {
             leftAvailable = false;
         }
     }
 }
 var hitRight: RaycastHit;
 if (Physics.Raycast(transform.position, transform.position + transform.right, hitRight, 1.9)) {
     for (var k = 0; k < obstacles.length; k++)
     {
         if (hitRight.collider.gameObject.name.IndexOf(obstacles[k]) > -1)
         {
             rightAvailable = false;
         }
     }
 }

So, am I right that when I want to check 2 units in front of the AI (transform.forward from the AI's point of view, not the global view!), I should use: Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9) ?

Comment
Add comment · Show 6
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 · Jan 27, 2013 at 05:47 AM 1
Share

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

 static function Raycast (origin : Vector3, direction : Vector3, distance : float = $$anonymous$$athf.Infinity, layer$$anonymous$$ask : int = kDefaultRaycastLayers) : boolean 

 Physics.Raycast(transform.position, transform.forward, hitFront, 1.9)

Direction is just transform.forward, the way you had it would absolutely do strange things !

avatar image sdgd · Jan 27, 2013 at 06:11 AM 0
Share

am why static raycast?

if he'll do for more than 1 AI all raycasts will use same raycast with same vars in it

well I've been just reading:

http://forum.unity3d.com/threads/52166-Static-Function

avatar image AlucardJay · Jan 27, 2013 at 08:05 AM 0
Share

If you are talking about this line :

 static function Raycast (origin : Vector3, direction : Vector3, distance : float = $$anonymous$$athf.Infinity, layer$$anonymous$$ask : int = kDefaultRaycastLayers) : boolean

that is directly from the API, stating that raycast is a static variable of a built-in class. Y'know, all the commands that make Unity work?!

avatar image fafase · Jan 27, 2013 at 08:17 AM 0
Share

As mentioned by @Alucardj your second parameter is an addition of your position and the forward. The proper is forward - position or even better only forward. Also, using transform to move is not best approach. You should prefer a character controller.

avatar image Joetjah · Jan 27, 2013 at 11:55 PM 0
Share

I've removed the Character Controller to check on a collision (Rigidbody). I wanted to use transform.Lerp to move the AI. Changing to your solution unfortunately still let the AI walk through walls. That should mean the Raycast is still uncorrect, am I right? Could it indeed be the Raycast being static gives problems when I try to do this for 4 AI's (all with the same script).

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Disabling a lineRender or Ray when there is no target 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Raycast collided object not transforming when scripted to? 1 Answer

Object reference not set to an instance of an object 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