Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DemonJAZ · Dec 23, 2018 at 11:27 AM · c#raycastcollider

I want ray cast in 4 directions, instead i am getting this.

alt text

This is my script for enemy direction , its just moving forwards and backward. Don't know why other rays are shattered too alt text strong text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyMove : MonoBehaviour
 {
     public float enemySpeed;
     enum Direction { FORWARD,BACKWARD,LEFT,RIGHT};
     Direction currentDirection = Direction.LEFT;
     bool directionSelected = false;
     // Update is called once per frame
     void Update()
     {
         if (directionSelected)
         {
             switch (currentDirection)
             {
                 case Direction.LEFT:
                     gameObject.transform.position = gameObject.transform.position + Vector3.left * enemySpeed * Time.deltaTime;
                     break;
 
                 case Direction.RIGHT:
                     gameObject.transform.position = gameObject.transform.position + Vector3.right * enemySpeed * Time.deltaTime;
                     break;
 
                 case Direction.FORWARD:
                     gameObject.transform.position = gameObject.transform.position + Vector3.forward * enemySpeed * Time.deltaTime;
                     break;
 
                 case Direction.BACKWARD:
                     gameObject.transform.position = gameObject.transform.position + Vector3.back * enemySpeed * Time.deltaTime;
                     break;
             }
         }
         
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         directionSelected = false;
         Debug.DrawLine(gameObject.transform.position, Vector3.left * 5f, Color.red,1000f);
         Debug.DrawLine(gameObject.transform.position, Vector3.right * 5f, Color.red, 1000f);
     Debug.DrawLine(gameObject.transform.position, Vector3.forward * 5f, Color.red, 1000f);
     Debug.DrawLine(gameObject.transform.position, Vector3.back * 5f, Color.red, 1000f);
 
 
         while (!directionSelected)
         {
             Random.seed = System.DateTime.Now.Millisecond;
             int checkDirection = Random.Range(1, 4);
             Debug.Log(checkDirection);
             
             switch (checkDirection)
             {
                 
                 case 1:
                     if (!Physics.Raycast(gameObject.transform.position, Vector3.left, 1f))
                     {
                         currentDirection = Direction.LEFT;
                         directionSelected = true;
                     }
                     break;
                 case 2:
                     if (!Physics.Raycast(gameObject.transform.position, Vector3.right, 1f))
                     {
                         currentDirection = Direction.RIGHT;
                         directionSelected = true;
                     }                    
                     break;
                 case 3:
                     if (!Physics.Raycast(gameObject.transform.position, Vector3.forward, 1f))
                     {
                         currentDirection = Direction.FORWARD;
                         directionSelected = true;
                     }
 
                     break;
                 case 4:
                     if (!Physics.Raycast(gameObject.transform.position, Vector3.back, 1f))
                     {
                         currentDirection = Direction.BACKWARD;
                         directionSelected = true;
                     }
                     break;               
             }
         }
 
 
     }
 }
 


capture.png (318.2 kB)
capture2.png (332.9 kB)
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

1 Reply

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

Answer by Bunny83 · Dec 23, 2018 at 11:51 AM

There are several issues here. First of all Random.Range(1, 4); will only produce the numbers 1, 2, 3 but never 4 since when using integer bounds the max value is exclusive. You would have to use Random.Range(1, 5);. But it's more common to use Random.Range(0, 4) where "4" represents the number of possible outcomes and it would produce 0, 1, 2, 3


The next issue if for some reason no direction is valid, your game will crash / hang because you can never leave the while loop.


A better approach would be to always do those 4 raycasts, push the results into a reused List and then pick a random element from that list. It has the advantage that each direction is only checked once. In your case since you randomly pick a direction it's very likely, if only one direction is valid, that you pick the same invalid direction several times (and in case of no valid you pick them infinitely many times == hang). This also simplifies the detection of "no possible move" because if the list is empty after the 4 raycasts there is no valid direction.


Finally your last issue is just a debugging issue. You do not visualize your raycasts properly. You used Debug.DrawLine. However DrawLine expects two worldspace positions but you passed one position and one direction. So the second point of your line will be basically a point 5 offset from the world origin. Your world origin is most likely somewhere at the 4th column and 3rd row. If you offset this by "5" in each lateral direction you end up at the points where the lines end.

you need to use either:

 Debug.DrawLine(transform.position, transform.position + Vector3.left * 5f);

or use DrawRay

 Debug.DrawRay(transform.position, Vector3.left * 5f);




The direction detection would be something like this:

 private List<Direction> availableDirections = new List<Direction>(4);
 
 private void OnCollisionEnter(Collision collision)
 {
     availableDirections.Clear();
     if (!Physics.Raycast(gameObject.transform.position, Vector3.left, 1f))
         availableDirections.Add(Direction.LEFT);
     if (!Physics.Raycast(gameObject.transform.position, Vector3.right, 1f))
         availableDirections.Add(Direction.RIGHT);
     if (!Physics.Raycast(gameObject.transform.position, Vector3.forward, 1f))
         availableDirections.Add(Direction.FORWARD);
     if (!Physics.Raycast(gameObject.transform.position, Vector3.back, 1f))
         availableDirections.Add(Direction.BACK);
 
     if (availableDirections.Count > 0)
     {
         currentDirection = availableDirections[Random.Range(0, availableDirections.Count)];
     }
     else
     {
         // Do something else, no direction to move
     }
 }


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

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

C# Door Script Problem 0 Answers

Raycast hit or miss in the same situation 1 Answer

RayCast From Centre of Camera 1 Answer

Raycast doesn't interact with Tilemap Collider when in a 3D scene. 0 Answers

How to make an object move to the direction in which the user faces using vr gaze interaction? 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