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 Catralitos · Jul 10, 2018 at 04:31 PM · 2dcollisioncollider2drpggrid based game

Trying to collide in grid like movement

Haven't played around with Unity in a while, this is probably simple, but I haven't tried the tutorials in a while. I'm trying to make a small FF/Dragon Quest like RPG. I'm trying to get a character to move, in a grid-like way, which I succeded thanks to a youtube tutorial. I'm having dificulties getting my character to not go through walls. This is a 2D project, I imported a map with Tiled2Unity, so the walls are Polygon Collider 2D. This is my PlayerMovement script so far.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public enum DIRECTION { UP, DOWN, LEFT, RIGHT }
 
 public class PlayerMovement : MonoBehaviour {
 
     public int speed = 5;
     private int buttonCooldown = 0;
     private bool canMove = true; 
     private bool moving = false;
     private DIRECTION dir = DIRECTION.DOWN;
     private Vector3 pos;
 
     // Use this for initialization
     void Start () {
         
     }
     
     void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "Wall")
             canMove = false;
     }
 
     // Update is called once per frame
     void Update () {
         buttonCooldown--;
         if (canMove) {
             pos = transform.position;
             move ();
         }
 
         if (moving) {
             if (transform.position == pos) {
                 moving = false;
                 canMove = true;
                 move ();
             }
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
         }
     }
 
     private void move(){
         if (buttonCooldown <= 0) {
             if (Input.GetKey (KeyCode.UpArrow)) {
                 if (dir != DIRECTION.UP) {
                     buttonCooldown = 5;
                     dir = DIRECTION.UP;
                 } else {
                     canMove = false;
                     moving = true;
                     pos += Vector3.up;
                 }
             } else if (Input.GetKey (KeyCode.DownArrow)) {
                 if (dir != DIRECTION.DOWN) {
                     buttonCooldown = 5;
                     dir = DIRECTION.DOWN;
                 } else {
                     canMove = false;
                     moving = true;
                     pos += Vector3.down;
                 }
             } else if (Input.GetKey (KeyCode.LeftArrow)) {
                 if (dir != DIRECTION.LEFT) {
                     buttonCooldown = 5;
                     dir = DIRECTION.LEFT;
                 } else {
                     canMove = false;
                     moving = true;
                     pos += Vector3.left;
                 }
             
             } else if (Input.GetKey (KeyCode.RightArrow)) {
                 if (dir != DIRECTION.RIGHT) {
                     buttonCooldown = 5;
                     dir = DIRECTION.RIGHT;
                 } else {
                     canMove = false;
                     moving = true;
                     pos += Vector3.right;
                 }
             }
         }
     }
 }

So, once my character hits a wall, he can't move anymore. It's probably really simple, just haven't worked with this in a while, trying to get some more training in the summer, so appreciate the help.

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
0
Best Answer

Answer by Catralitos · Jul 11, 2018 at 09:07 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public enum DIRECTION { UP, DOWN, LEFT, RIGHT }
 
 public class PlayerMovement : MonoBehaviour {
 
     public int speed = 5;
     private int buttonCooldown = 0;
     private bool canMove = true; 
     private bool moving = false;
     private DIRECTION dir = DIRECTION.DOWN;
     private Vector3 pos;
 
     // Use this for initialization
     void Start () {
         
     }
   
     // Update is called once per frame
     void Update () {
         buttonCooldown--;
         if (canMove) {
             pos = transform.position;
             move ();
         }
 
         if (moving) {
             if (transform.position == pos) {
                 moving = false;
                 canMove = true;
                 move ();
             }
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
         }
     }
 
     private void move(){
         if (buttonCooldown <= 0) {
             if (Input.GetKey (KeyCode.UpArrow)) {
                 if (dir != DIRECTION.UP) {
                     buttonCooldown = 5;
                     dir = DIRECTION.UP;
                 } else if (noWall(DIRECTION.UP)){
                     canMove = false;
                     moving = true;
                     pos += Vector3.up;
                 }
             } else if (Input.GetKey (KeyCode.DownArrow)) {
                 if (dir != DIRECTION.DOWN) {
                     buttonCooldown = 5;
                     dir = DIRECTION.DOWN;
                 } else if (noWall(DIRECTION.DOWN)){
                     canMove = false;
                     moving = true;
                     pos += Vector3.down;
                 }
             } else if (Input.GetKey (KeyCode.LeftArrow)) {
                 if (dir != DIRECTION.LEFT) {
                     buttonCooldown = 5;
                     dir = DIRECTION.LEFT;
                 } else if (noWall(DIRECTION.LEFT)) {
                     canMove = false;
                     moving = true;
                     pos += Vector3.left;
                 }
             
             } else if (Input.GetKey (KeyCode.RightArrow)) {
                 if (dir != DIRECTION.RIGHT) {
                     buttonCooldown = 5;
                     dir = DIRECTION.RIGHT;
                 } else if (noWall(DIRECTION.RIGHT)){
                     canMove = false;
                     moving = true;
                     pos += Vector3.right;
                 }
             }
         }
     }
 
     private bool noWall(DIRECTION dir){
         RaycastHit2D hitUp = Physics2D.Raycast(transform.position, Vector2.up, 1);
         RaycastHit2D hitDown = Physics2D.Raycast(transform.position, Vector2.down, 1);
         RaycastHit2D hitRight = Physics2D.Raycast(transform.position, Vector2.right, 1);
         RaycastHit2D hitLeft = Physics2D.Raycast(transform.position, Vector2.left, 1);
         if (dir == DIRECTION.UP && hitUp.collider == null){
             return true;
         }
         if (dir == DIRECTION.DOWN && hitDown.collider == null){
             return true;
         }
         if (dir == DIRECTION.LEFT && hitLeft.collider == null){
             return true;
         }
         if (dir == DIRECTION.RIGHT && hitRight.collider == null){
             return true;
         } 
         return false;
     }
 
 }
 

This fixed the problem. Just mae sure to disable the Collider so the Raycast will work.

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 ShadyProductions · Jul 11, 2018 at 09:04 AM 1
Share

Why do you execute all the raycasts if u only need it for one direction?

     private bool noWall(DIRECTION dir)
     {
         RaycastHit2D hit;
         if (dir == DIRECTION.UP)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.up, 1);
             return hit.collider == null;
         }
         if (dir == DIRECTION.DOWN)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.down, 1);
             return hit.collider == null;
         }
         if (dir == DIRECTION.LEFT)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.left, 1);
             return hit.collider == null;
         }
         if (dir == DIRECTION.RIGHT)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.right, 1);
             return hit.collider == null;
         }
         return false;
     }
avatar image Catralitos ShadyProductions · Jul 11, 2018 at 09:45 AM 0
Share

To be honest, not a raycast expert, just adapted another script I found online for a simillar iusse. Gonna chaage to this, it is better thank you.

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

277 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

Related Questions

2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer

Circle Collider2D Stuck ob Box Collider2D at the corner :( 0 Answers

Inconsistent 2d collider hit registration 3 Answers

2D colliders doesnt work with Gravity Scale = 0 0 Answers

Sprite 2d passes through the ground 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