• 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 · Oct 01, 2014 at 05:14 PM · 2draycastarrayturn

How to move like Pacman/Snake using Raycasts inside a 2D array ?

I have a grid based level with each grid sized 1x1 unit(default). How do I make my gameobject move through the lanes accelerating in a specific direction (Pacman) until an input is given to change its' direction, or if it collides with a deadend wall and turns to its' right ? The movement needs to be aligned to the middle of the tile. An example of the level with the player object is given below. The green object is the player and red cylinder is a goal, and the scenario in the picture is when no input button was pressed, hereby allowing the player object to auto-turn left.

alt text

Comment
Add comment · Show 1
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 Dark_Tiger · Oct 28, 2014 at 08:51 PM 0
Share

Raycast forward, then if distance to object if less than 1 or whatever, change direction.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Oct 01, 2014 at 06:23 PM

Pacman movement is done using a 2D array of possible positions. I would build an array for each level, then use that array for collision detection.

Comment
Add comment · Show 4 · 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 · Oct 01, 2014 at 06:37 PM 0
Share

I had a working version of a script that does exactly that. However, the movement was happening only when a direction key was pressed, making it a manual movement, ins$$anonymous$$d of automatic movement. Also, getting the auto turn to work is making me go nuts. Here is that script. $$anonymous$$indly note that a grid inside the array with a value of 1 is the unwalkable tile.

 public GameObject levelCreator_;
     levelCreator temp;
     Vector3 playerPos;
     
     int[,] mapArray = new int[13,17];
     public bool inhibitPlayerInput;
 
 void Start () {
         DOTween.Init(true, true, LogBehaviour.Verbose).SetCapacity(6000, 6000);
 
         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)     // tile with value 2 is the starting position of the player
                 {
                     playerPos = new Vector3(i,ii,0);
                 }
             }
         }
         
         transform.position = new Vector3(playerPos.x,playerPos.y,0);
     }
 
 void Update () {
         if (inhibitPlayerInput) return;
         getInput();
     }
 
 void getInput()
     {
         bool inputPressed = false;
         Vector3 newPlayerPos = playerPos;
         
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
         {
             inputPressed = true;
             newPlayerPos += new Vector3(-1,0,0);
         } 
         else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
         {
             inputPressed = true;
             newPlayerPos += new Vector3(1,0,0);
         } 
         else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
         {
             inputPressed = true;
             newPlayerPos += new Vector3(0,-1,0);
         }
         else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
         {
             inputPressed = true;
             newPlayerPos += new Vector3(0,1,0);
         } 
         
         if (!inputPressed) return;
         
         if (mapArray[(int)newPlayerPos.x,(int)newPlayerPos.y] == 1)      // Unwalkable tile check
         {
             return;
         }
         playerPos = newPlayerPos;
         
         if(mapArray[(int)playerPos.x,(int)playerPos.y] == 0 || mapArray[(int)playerPos.x,(int)playerPos.y] >= 2)
         {
             inhibitPlayerInput = true;
             transform.DO$$anonymous$$ove(playerPos, TweenWpDuration).OnComplete(() => inhibitPlayerInput = false).SetEase(Ease.Linear);
             return;
         }
     }
avatar image coolbird22 · Oct 02, 2014 at 04:21 AM 0
Share

Anyone have any clues about this ?

avatar image snabbott · Oct 28, 2014 at 08:47 PM 0
Share

I'm using a RigidBody2D and velocity ins$$anonymous$$d of position. To make it easier on myself, I set up velocity vectors named "up", "down", "left", "right", and "stop".

avatar image Alexetify · Nov 07, 2016 at 03:01 PM 0
Share

snabbott , can you share your code for pacman moving, please.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Test line renderer collision? 1 Answer

Clicking on GameObject in 2D 3 Answers

How to remove gaps in a 2D map. 0 Answers

Raycast Physics2D not working as expected 1 Answer

[2D] Find nonobscured corners of 2D sprites or collision boxes? 0 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