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 coolbird22 · Sep 10, 2014 at 12:30 PM · raycastgridlevelrayhit

What is the best way to navigate through a grid based level using Raycasting ?

I have a level with tiles of 1 unit each. How do I make sure that the player(lavender) never goes into the walls and always stays in the middle of the walkable area(steel blue) ? I have tried to put nodes(red) which, if hit by a raycast ray will allow the turn to happen, but this method fails when the player is at a high speed.

Check the level image here: alt text

Check my code here:

     RaycastHit hit;
     Ray wRay;
     Ray aRay;
     Ray sRay;
     Ray dRay;
 
 void Update () {
 transform.position += transform.forward * speed * Time.deltaTime;
 
 if (Input.GetKey (KeyCode.W)) {
             wRay.origin = transform.position;
             wRay.direction = Vector3.left;
             if (Physics.Raycast(wRay, out hit, 2f) && hit.transform.tag == "turnallowed")
             {
                     gameObject.transform.rotation = Quaternion.Euler(0,270,0);
             }
             Debug.DrawRay(transform.position, Vector3.left, Color.red);
         }
 
 
         if (Input.GetKey (KeyCode.A)) {
             aRay.origin = transform.position;
             aRay.direction = Vector3.back;
             if (Physics.Raycast(aRay, out hit, 2f) && hit.transform.tag == "turnallowed")
             {
                 gameObject.transform.rotation = Quaternion.Euler(0,180,0);
             }
             Debug.DrawRay(transform.position, Vector3.back, Color.blue);
         }
 
 
         if (Input.GetKey (KeyCode.S)) {
             sRay.origin = transform.position;
             sRay.direction = Vector3.right;
             if (Physics.Raycast(sRay, out hit, 2f) && hit.transform.tag == "turnallowed")
             {
                 gameObject.transform.rotation = Quaternion.Euler(0,90,0);
             }
             Debug.DrawRay(transform.position, Vector3.right, Color.white);
         }
 
         if (Input.GetKey (KeyCode.D)) {
             dRay.origin = transform.position;
             dRay.direction = Vector3.forward;
             if (Physics.Raycast(aRay, out hit, 2f) && hit.transform.tag == "turnallowed")
             {
                 gameObject.transform.rotation = Quaternion.Euler(0,0,0);
             }
             Debug.DrawRay(transform.position, Vector3.forward, Color.green);
         }
 
 }

tt.png (19.5 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dterbeest · Sep 10, 2014 at 02:25 PM

The best way IMHO would be to keep an occupied flag in the grid itself..

 class Cell {
   public bool occupied;
 }
 
 class Grid {
   public Cell[,] cells = new Cell[10,10];
 }
 
 class Move {
   void Update() {
     if (Input.GetKeyDown(Keycode.W)) { //move left
       if (Grid.cells[currentX +1, currentY].occupied)
         //false move
     } //etc...
   }
 }



This is a very short example, but i think you get the idea

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 coolbird22 · Sep 10, 2014 at 03:07 PM 0
Share

Working with arrays is exactly what I had done before and I had gotten it to work just fine, with the only exception that the player was basically teleporting into the empty space ins$$anonymous$$d of moving at a speed. And if it sees that the next grid is a wall, it will set the position of the player where it is and wont move. But, a problem that arose is that when I was making the player slide, and if the player moved onto the wall grid, it was only after reaching that grid, that the grid check was happening, and then it sets the position back to the previous grid. To be clear, even if I move into the wall grid, it is only after completing the movement, that the check comes into play and sets the movement back to the previous grid. Here is the script I was using:

 using UnityEngine;
 using System.Collections;
 
 public class player : $$anonymous$$onoBehaviour {
 
     public GameObject levelCreator_;
     levelCreator temp;
     public Vector2 playerPos;
     
     int[,] mapArray = new int[13,17];
 
     void Start () {
         levelCreator_ = GameObject.Find ("LevelCreatorGameObject");
         temp = levelCreator_.gameObject.GetComponent<levelCreator>();
         mapArray = temp.mapArray;
         for(int i = 1; i<12; i++)
         {
             for(int ii = 1; ii < 16; ii++)
             {
                 if( mapArray[i,ii] == 2)
                 {
                     playerPos = new Vector2(i,ii);
                 }
             }
         }
         transform.position = new Vector3(playerPos.x,0,playerPos.y);
     }
     
     void Update () {
         if (gameObject.CompareTag("alive"))
             getInput();
     }
     
     void getInput()
     {
         Vector2 oldPos = playerPos;
 
         if(Input.Get$$anonymous$$eyDown("up"))
         {
             playerPos += new Vector2(-1,0);
         }
         if(Input.Get$$anonymous$$eyDown("down"))
         {
             playerPos += new Vector2(1,0);
         }
         if(Input.Get$$anonymous$$eyDown("left"))
         {
             playerPos += new Vector2(0,-1);
         }
         if(Input.Get$$anonymous$$eyDown("right"))
         {
             playerPos += new Vector2(0,1);
         }
         
         if(mapArray[(int)playerPos.x,(int)playerPos.y] == 1) //typecast as vector2 stores floats
         {
             playerPos = oldPos;
         }
         transform.position = new Vector3(playerPos.x,0,playerPos.y);
     }
 }
avatar image
0

Answer by Jan_Julius · Sep 10, 2014 at 01:35 PM

Why are you having a grid based level when you are accelerating?

Why not just have the raycasts look for walls and if there is a wall you cant use that button to move the way the raycast is raying?

Comment
Add comment · Show 2 · 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 coolbird22 · Sep 10, 2014 at 02:11 PM 0
Share

By grid based level, I meant that the tiles are manually placed in a grid fashion. Apologies if I didn't explain it better. I'll try looking for walls ins$$anonymous$$d. But I guess this still will make the player not align in the middle of the lane. I'm also not sure how to make the player turn sideways by itself if it touches a wall, headlong.

avatar image Jan_Julius · Sep 10, 2014 at 02:37 PM 0
Share

By turning you mean rotating the object? If you resize the cube a little so it doesn't touch the wall you can rotate it, you can also make it rotate first and then letting it move when the button gets pressed again so you make sure the object is facing that way by checking its rotation.

Something I've done before is giving it a status, whatever the status is in corresponds with it's rotation. So let's say pressing D will make you face to the right and rotate your object 270 degrees you can do something like this:

 public int status;

 void FixedUpdate(){
         switch(status){
         case 0:
             gameObject.transform.eulerAngles = new Vector3(
                 0, 0, 0);
             break;
         case 1:
             gameObject.transform.eulerAngles = new Vector3(
                 0, 0, 90);
             break;
         case 2:
             gameObject.transform.eulerAngles = new Vector3(
                 0, 0, 180);
             break;
         case 3:
             gameObject.transform.eulerAngles = new Vector3(
                 0, 0, 270);
             break;
         }
           }



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

25 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

Related Questions

Trouble with Ray & Raycasting and AddForce() 1 Answer

How to detect if a raycast ray stop hitting an object 1 Answer

How to get information from raycast? 2 Answers

Fall collision force with help of raycast 0 Answers

I don't know why i can't detect by ray sth. tagged, 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