• 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
Question by DubstepDragon · Dec 30, 2014 at 10:19 PM · collisionmovementgridmanual

Issues with manual collision detection involving updates...

My manual collision system involves four scripts attached to four triggers around the character object, which stop movement in that direction when colliding with a "wall"-tagged object. The game is grid-based, so this is a good way to maintain said collision. When the character walks towards a wall, he stops, as I have scripted it. However, when his side is touching the wall and he rotates to face it, the trigger in front does not detect the wall, and the player can walk through the wall. Is there a possible way to fix this? Does it perhaps have to do with updates?

I have:

  • Made sure they all have RigidBody attached to them

  • Referred to documentation for this issue

Here is my movement script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public static bool CanMoveForward;
     public static bool CanMoveBackward;
     public static bool CanMoveLeft;
     public static bool CanMoveRight;
 
     //Translation:
     float movSpeed = 4.0f;
     Vector3 pos;
     Transform tr ;
     public static bool moving = false;
     
     //Rotation:
     public static bool rotating = false;
     public float rotSpeed = 360f;
     float rotDegrees = 0f;
     Quaternion rotToAngle ;
     
     void Start () {  
         pos = transform.position;
         tr = transform;
 //        CanMoveForward = true;
 //        CanMoveBackward = true;
 //        CanMoveLeft = true;
 //        CanMoveRight = true;
     }
     
     // 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 && CanMoveRight) {
                 //pos += Vector3.right;
                 pos += transform.right;
                 moving=true;
 //                print ("MOVE LEFT");
             } else if (Input.GetKey(KeyCode.A) && tr.position == pos && CanMoveLeft) {
                 pos += -transform.right;
                 moving=true;
 //                print ("MOVE RIGHT");
             } else if (Input.GetKey(KeyCode.W) && tr.position == pos && CanMoveForward) {
                 pos += transform.forward;
                 moving=true;
 //                print ("MOVE FORWARD");
             } else if (Input.GetKey(KeyCode.S) && tr.position == pos && CanMoveBackward) {
                 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);
             }
         }
     }
 
 }
 



=======================================

Here are the scripts that control halting movement relative to direction:

 using UnityEngine;
 using System.Collections;
 
 public class FrontCollisionSystem : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         PlayerMovement.CanMoveForward = true;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnTriggerEnter(Collider other) {
         if(other.CompareTag("wall") && !PlayerMovement.rotating) {
             PlayerMovement.CanMoveForward = false;
         }
     }
 
     void OnTriggerExit(Collider other) {
         PlayerMovement.CanMoveForward = true;
     }
 
 }
 




 using UnityEngine;
 using System.Collections;
 
 public class BackCollisionSystem : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         PlayerMovement.CanMoveBackward = true;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnTriggerEnter(Collider other) {
         if(other.CompareTag("wall") && !PlayerMovement.rotating) {
             PlayerMovement.CanMoveBackward = false;
         }
     }
 
     void OnTriggerExit(Collider other) {
         PlayerMovement.CanMoveBackward = true;
     }
 
 }
 




 using UnityEngine;
 using System.Collections;
 
 public class LeftCollisionSystem : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         PlayerMovement.CanMoveLeft = true;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnTriggerEnter(Collider other) {
         if(other.CompareTag("wall") && !PlayerMovement.rotating) {
             PlayerMovement.CanMoveLeft = false;
         }
     }
 
     void OnTriggerExit(Collider other) {
         PlayerMovement.CanMoveLeft = true;
     }
 
 }
 





 using UnityEngine;
 using System.Collections;
 
 public class RightCollisionSystem : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         PlayerMovement.CanMoveRight = true;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnTriggerEnter(Collider other) {
         if(other.CompareTag("wall") && !PlayerMovement.rotating) {
             PlayerMovement.CanMoveRight = false;
         }
     }
 
     void OnTriggerExit(Collider other) {
         PlayerMovement.CanMoveRight = true;
     }
 
 }
 





Thanks in advance! :D

Comment

People who like this

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

Answer by Oliver1135 · Dec 31, 2014 at 12:55 AM

You've a lot of code but I may have spotted what your issue is here:

   void OnTriggerEnter(Collider other) {
          if(other.CompareTag("wall") && !PlayerMovement.rotating) {
              PlayerMovement.CanMoveForward = false;
          }
      }

this I believe is where if the player is rotating and up against a wall, I assume the CanMoveForward is not being set to false and thus allowing him to pass through the wall

Comment
DubstepDragon

People who like this

1 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 DubstepDragon · Dec 31, 2014 at 11:45 AM 0
Share

Thanks very much! Seems like the " && !PlayerMovement.rotating" was unnecessary. It works now, thanks a lot! :D

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

26 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

Related Questions

Adding collision detection to movement script... 1 Answer

Manual Collision Detection through triggers... 0 Answers

Faults with Grid-Based movement... 2 Answers

Stop player running at wall 1 Answer

A node in a childnode? 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