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 ASJSF911 · Oct 02, 2016 at 07:55 PM · unity 52d-physics

2d Grid movement collision errors,raycast detecting walls grid movement

i have a 2d grid rpg type playermovement script that i am working on(2nd one im working on gave up on the first due to collision issues) i figured away of preventing my player from walking into the walls or through, or in my last experience, walking into the collision area and then bouncing back. I got this working perfectly in all directions except on direction which is down. please see my attached script. Like i said all directions work perfectly fine except one..

 public class Movement : MonoBehaviour {
     public enum DIRECTION{
         UP,
         DOWN,
         LEFT,
         RIGHT
     }
 
     private Animator _anim;
     public DIRECTION DirFacing = DIRECTION.DOWN;
     public DIRECTION OldDirection = DIRECTION.DOWN;
     public Vector3 MoveStartPoint;
     public Vector3 MoveEndPoint;
     public float TileSize = 16;
     public float MoveSpeed;
     public float increment;
     public bool isMoving;
     public bool AbleToMove;
     // Use this for initialization
     void Start () {
         _anim = GetComponent<Animator>();
     }
 
 
     void LateUpdate(){
         if(isMoving){
             transform.position = Vector3.Lerp(MoveStartPoint,MoveEndPoint,increment);
 
         }
     }
     // Update is called once per frame
     void Update () {
     
         if(AbleToMove){
             if(increment <= 1 && isMoving){
                 increment += MoveSpeed/100;
             }
             else{
                 if(DirFacing == DIRECTION.UP){ _anim.Play("StillUp"); }
                 if(DirFacing == DIRECTION.DOWN){ _anim.Play("StillDown"); }
                 if(DirFacing == DIRECTION.LEFT){ _anim.Play("StillLeft"); }
                 if(DirFacing == DIRECTION.RIGHT){ _anim.Play("StillRight"); }
                 isMoving = false;
             }
 
             if(Input.GetKey(KeyCode.UpArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z))){
                 //RaycastHit hit = Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z));
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.UP;
                 _anim.Play ("WalkingUp");
             }
 
 
             if(Input.GetKey(KeyCode.DownArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y - TileSize, transform.position.z))){
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x,transform.position.y-TileSize , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.DOWN;
                 _anim.Play("WalkingDown");
             }
 
 
             if(Input.GetKey(KeyCode.LeftArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x - TileSize, transform.position.y, transform.position.z))){
 
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x-TileSize,transform.position.y , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.LEFT;
                 _anim.Play("WalkingLeft");
             }
 
 
             if(Input.GetKey(KeyCode.RightArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x + TileSize, transform.position.y, transform.position.z))){
 
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x+TileSize,transform.position.y , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.RIGHT;
                 _anim.Play("WalkingRight");
             }
                 
         }
 
     }
 }
 

,first off i have a decent amount of programming experience but ive never really attempted to make atop down 2d RPG game. for the most part im getting it. I am working in a 3d environment still as well so you are aware. and also working in C#. I have got a tiled map setup, and my boxcolliders(not boxcollider2d) set up for my walls currently. i have just figured out how to prevent my player from walking into those walls using the following statement:

 if(Input.GetKey(KeyCode.UpArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z))){

and this is working perfectly fine in all directions... Except for when I try pressing down. my player will not move at all. my down button call is identical except for the only changes being KeyCode.DownArrow and in the new vector3, my Y value is set to - TileSize as can be seen below

 if(Input.GetKey(KeyCode.DownArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y - TileSize, transform.position.z))){

any advise would be much appreciated.. i gave up onmy first project because of this collision detection was going well. I kept getting my player walking into the collision area and then bouncing back.

below is my whole movement script

 public class Movement : MonoBehaviour {
     public enum DIRECTION{
         UP,
         DOWN,
         LEFT,
         RIGHT
     }
 
     private Animator _anim;
     public DIRECTION DirFacing = DIRECTION.DOWN;
     public DIRECTION OldDirection = DIRECTION.DOWN;
     public Vector3 MoveStartPoint;
     public Vector3 MoveEndPoint;
     public float TileSize = 16;
     public float MoveSpeed;
     public float increment;
     public bool isMoving;
     public bool AbleToMove;
     // Use this for initialization
     void Start () {
         _anim = GetComponent<Animator>();
     }
 
 
     void LateUpdate(){
         if(isMoving){
             transform.position = Vector3.Lerp(MoveStartPoint,MoveEndPoint,increment);
 
         }
     }
     // Update is called once per frame
     void Update () {
     
         if(AbleToMove){
             if(increment <= 1 && isMoving){
                 increment += MoveSpeed/100;
             }
             else{
                 if(DirFacing == DIRECTION.UP){ _anim.Play("StillUp"); }
                 if(DirFacing == DIRECTION.DOWN){ _anim.Play("StillDown"); }
                 if(DirFacing == DIRECTION.LEFT){ _anim.Play("StillLeft"); }
                 if(DirFacing == DIRECTION.RIGHT){ _anim.Play("StillRight"); }
                 isMoving = false;
             }
 
             if(Input.GetKey(KeyCode.UpArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z))){
                 //RaycastHit hit = Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z));
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3 (transform.position.x, transform.position.y + TileSize, transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.UP;
                 _anim.Play ("WalkingUp");
             }
 
 
             if(Input.GetKey(KeyCode.DownArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x, transform.position.y - TileSize, transform.position.z))){
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x,transform.position.y-TileSize , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.DOWN;
                 _anim.Play("WalkingDown");
             }
 
 
             if(Input.GetKey(KeyCode.LeftArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x - TileSize, transform.position.y, transform.position.z))){
 
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x-TileSize,transform.position.y , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.LEFT;
                 _anim.Play("WalkingLeft");
             }
 
 
             if(Input.GetKey(KeyCode.RightArrow) && isMoving == false && !Physics.Linecast (transform.position, new Vector3 (transform.position.x + TileSize, transform.position.y, transform.position.z))){
 
                 isMoving = true;
                 increment = 0;
                 MoveStartPoint = transform.position;
                 MoveEndPoint = new Vector3(transform.position.x+TileSize,transform.position.y , transform.position.z);
                 OldDirection = DirFacing;
                 DirFacing = DIRECTION.RIGHT;
                 _anim.Play("WalkingRight");
             }
                 
         }
 
     }
 }
 

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

Answer by gabriellp21 · Oct 04, 2016 at 02:31 PM

Hello! First of anything i ever use OnDrawGizmos on unity to see where i'am casting lines to be sure about the direction. My opinion is to you setup a debug code with gizmos to see if your cast realy is pointing to the correct direction and distance. Another thing is to try use math constants like Vector3.up, Vector3.down, etc. To project casts direction. I hope to be helpful. Any question send a message :).

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

116 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

Related Questions

EventSystem sending event to further object 1 Answer

Unity 2D: Interpolate object collision stuck 1 Answer

Physics2D Optimize Physics.UpdateBodies. 0 Answers

Player can only jump once!? 0 Answers

2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 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