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 29, 2014 at 11:07 PM · movementgridbased

Manual Collision Detection through triggers...

Here's my movement script, it moves in directions by 1 unit. I tried to implement bools that allow movement in the four directions, but I came across an issue... As of now, my character does collide with an object tagged as "wall" when facing it... That is because there is a trigger in front of the character that detects when a wall is directly forward as its OnTriggerEnter function is run. This works to an extent. If I were to use my method, I would need four different triggers for all sides of the character (a cube). They would ideally be placed as children to the player in the Hierarchy, and so I would need to know how I can access a child's component (the box collider, and the child being an empty preferably.)

Now, I can either carry out with my approach with some help from the community, or someone can suggest a better way if there is any. Both would be extremely appreciated.

Here is my movement script that has the manual collision detection:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public bool CanMoveForward;
     public bool CanMoveBackward;
     public bool CanMoveLeft;
     public bool CanMoveRight;
 
     //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;
         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);
             }
         }
     }
 
     void OnTriggerEnter(Collider other) {
         if(other.CompareTag("wall")) {
             Debug.Log("Collided with 'wall'");
             CanMoveForward = false;
         }
     }
 
     void OnTriggerExit(Collider other) {
         CanMoveForward = true;
     }
 }
 



Thanks in advance! :D

Comment
Add comment · Show 3
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 29, 2014 at 11:09 PM 0
Share

Note: The only one working is the frontal collision as a GameObject can only have one collider/trigger that is accessed by a script independently. What I want to do is have children with four different colliders. :D

avatar image taxvi · Dec 30, 2014 at 01:30 PM 0
Share

mm... I'd make one big trigger around the player and whenever it collides with something I'd take the relative position of the obstacle to see which way I can move

avatar image hav_ngs_ru · Dec 30, 2014 at 02:21 PM 1
Share

and what is the problem? make a child for every direction and attach trigger to each of them... to acces them use GetComponentsInChildren and check some publics to detech what direction this trigger for.

or make public fields for every direction and drag-drop children with triggers to them.

or you can do as @taxvi said :)

0 Replies

· Add your reply
  • Sort: 

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

Adding collision detection to movement script... 1 Answer

A node in a childnode? 1 Answer

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

Grid based movement - using transform.translate 3 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