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 DubstepDragon · Dec 24, 2014 at 06:17 PM · collisionmovementgridbased

Adding collision detection to movement script...

I found a great script on the forums and modified it slightly to do my bidding; I am attempting to create a grid-based movement system, similar to that of classic games such as Eye of the Beholder. The system is working out fine in terms of movement, yet it does not detect collision, and completely stops functioning when a RigidBody component is added to my character (who is just a cube with a camera). I was wondering how collision detection may be carried out in the sense that moving when stood in front of a wall is not permitted, instead of just smashing into the wall and destroying the metric offset of the project.

Here is the script I am using:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     //Translation:
     float movSpeed = 4.0f;
     Vector3 pos;
     Transform tr ;
     bool moving = false;
     
     //Rotation:
     bool rotating = false;
     public float rotSpeed = 360f;
     float rotDegrees = 0f;
     Quaternion rotToAngle ;
     
     void Start () {  
         pos = transform.position;
         tr = transform;
     }
     
     // Update is called once per frame
     void Update () {
         Debug.DrawRay(transform.position, transform.forward, Color.red);
         //Input:
         if (!moving && !rotating) {
             if (Input.GetKey(KeyCode.D) && tr.position == pos) {
                 //pos += Vector3.right;
                 pos += transform.right;
                 moving=true;
 //                print ("MOVE LEFT");
             } else if (Input.GetKey(KeyCode.A) && tr.position == pos) {
                 pos += -transform.right;
                 moving=true;
 //                print ("MOVE RIGHT");
             } else if (Input.GetKey(KeyCode.W) && tr.position == pos) {
                 pos += transform.forward;
                 moving=true;
 //                print ("MOVE FORWARD");
             } else if (Input.GetKey(KeyCode.S) && tr.position == pos) {
                 pos += -transform.forward;
                 moving=true;
 //                print ("MOVE BACK");
             } else if (Input.GetKey(KeyCode.Q) && tr.position == pos) {
                 rotDegrees -= 90f;
                 //rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
 //                print ("TURN LEFT");
             } else if (Input.GetKey(KeyCode.E) && tr.position == pos) {
                 rotDegrees += 90f;
                 //rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
 //                print ("TURN RIGHT");
             }
         }
         
         //Translation:
         if (moving) {
             if (Vector3.Distance(transform.position,pos) <0.05f){
                 transform.position = pos;
                 moving=false;
 //                print ("FINISHED MOVE");
             } else {
                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
             }
         }
         
         //Rotation:
         if (rotating) {
             if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
                 transform.rotation = rotToAngle;
                 rotating=false;
 //                print ("FINISHED TURN");
             } else {
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
             }
         }
     }
 }
 


Thanks in advance!

Comment
Add comment · Show 4
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 _dns_ · Dec 24, 2014 at 07:07 PM 1
Share

Hi, well, at the time of "Eye of the Beholder", there was very limited CPU and RA$$anonymous$$ so collisions were managed using 2D grids = 2 dimension tables of bytes or integers containing flags. Example: you could represent the "collision world" (= not the graphic one) by a "$$anonymous$$yEnum $$anonymous$$yTable[,]". $$anonymous$$yEnum would be an enum/bitfield that could be "floor", "wall", "trap" etc... Players and NPC would check collisions by checking if the element of the table they want to go is a wall or not. Level design could be done using a simple text file loaded in this table... Now, things gets more complicated than that but that's a start :-)

avatar image DubstepDragon · Dec 27, 2014 at 12:31 PM 0
Share

You've got me all curious and interested!

Can you please give me a simple example and how it could be done in Unity3D?

Thanks a lot! :D

Edit: I'd like, if possible, the enumeration to have two values, one for no collision and the second for collision. I'd program every other thing (traps, etc...) through triggers and simple scripts.

avatar image _dns_ · Dec 27, 2014 at 03:51 PM 1
Share

Here are some resources about loading a text file in a table to represent the level design: http://www.michaeljohnstephens.com/devblog/creating-storing-and-loading-levels-with-text-files-in-unity or http://answers.unity3d.com/questions/577889/create-level-based-on-xmltxt-file.html

The table will be the "logical" world, with coordinates like mytable[i,j] with i, j being integers. The "graphical world" can be instantiated based on this logical world at coordinates represented by Vector3((float)i, 0.0f, (float)j). This is the same concept as a platformer or 2D game, with 3D elements ins$$anonymous$$d of 2D sprites to represent the graphical world.

avatar image DubstepDragon · Dec 29, 2014 at 07:43 PM 0
Share

Would be awesome if you can convert your comment to an answer, I'd mark it as accepted :D

1 Reply

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

Answer by Mmmpies · Dec 29, 2014 at 09:15 PM

Really all that text file stuff is creating an array based on a text file.

So all you need is an array to store where your walls are, lets say free space to move to = 0 and a wall = 1.

 using UnityEngine;
 using System.Collections;
 
 public class MapArray : MonoBehaviour {
 
     public int[,] mapArray = new int[6,6] {
         {1, 1, 0, 1, 1, 1},
         {1, 0, 0, 1, 0, 1},   // start point xPos = 1 (this line) yPos = 4 (5th number)
         {1, 0, 1, 1, 0, 1},
         {1, 0, 1, 0, 0, 1},
         {1, 0 ,0, 0 ,1, 1},
         {1, 1, 1, 1, 1, 1}
     };
 
     private int xPos;
     private int yPos;
 
     void Start()
     {
         xPos = 1;
         yPos = 4;
         Debug.Log ("Starting on a point that == " + mapArray[xPos,yPos]);
     }
 
 }
 

Basic I know but before you move onto the next place check that place == 0 and not 1. You'll need to establish which way you're facing but if you intend to move down then check mapArray[xPos + 1, yPos] and if = 0 move, if = 1 don't move.

You could also add other numbers that reference traps or gold. Sorry I'm not familiar with Eye of the Beholder so don't know if you want those things in your game.

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

28 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

Related Questions

Faults with Grid-Based movement... 2 Answers

Issues with manual collision detection involving updates... 1 Answer

Manual Collision Detection through triggers... 0 Answers

Stop player running at wall 1 Answer

Grid based movement collision detection 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