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 NickelBuckle9 · Oct 25, 2012 at 01:30 AM · c#collisiononcollisionenterprevent

Simple collision?

Hello!

I'm writing a fairly basic game to learn C# with, so I didn't want to use any pre-made assets or scripts. I have a fps control system and everything is working great!

The player walks around a simple square room, and obviously shouldn't be able to go through the walls. I can use OnCollisionEnter and OnCollisionExit, and they both work at detecting when the collider has intersected with something. However, this does not prevent the character from going through the walls and objects.

Is there an easy way to prevent 2 game objects from going through each other?

I can think of a few ways to do accomplish/fake the effect I'd like, such as only allowing movement in a direction up to a certain value, but I feel like it wouldn't be useful in an actual game, because I won't always be dealing with prefect squares.

What would be the 'official' way of preventing an object from intersecting?

Comment
Add comment · Show 6
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 Eric5h5 · Oct 25, 2012 at 02:11 AM 0
Share

Any reason you're not using a character controller?

avatar image NickelBuckle9 · Oct 25, 2012 at 03:06 AM 0
Share

Yes, the purpose of this project is to help me learn C#, not to make a fancy, functioning game. Using the character controller wouldn't help me :)

avatar image Eric5h5 · Oct 25, 2012 at 03:14 AM 0
Share

Not sure how that follows; using a character controller is part of learning Unity and C#. Nothing to do with making a functioning game--use the appropriate tools for the job.

avatar image NickelBuckle9 · Oct 25, 2012 at 03:25 AM 0
Share

True, the character controller is helpful and would make the job easier, but I've already played around with it for a bit the past few days. I know there's no point in reinventing the wheel, but for the sake of trying to think in terms of a program/game, I feel it would be more helpful to come up with my own solution. I guess I could probably set the translation of the character in the normal direction equal to 0 while contact with the wall is true. That could work... right?

avatar image Eric5h5 · Oct 25, 2012 at 03:51 AM 0
Share

Aside from the character controller, collision generally works using rigidbodies and adding forces, not directly translating, except for Rigidbody.$$anonymous$$ovePosition.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by N1GHTMAR3 · Oct 25, 2012 at 05:31 AM

If OnCollisionEnter and OnCollisionExit is working, i think you are using Translate to move the player. To prevent the player from going through the walls you must add character controller component to your player and use CharacterController.move instead.

This code is from Unity Scripting Reference.

PD: Excuse my english.


 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
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
avatar image
0

Answer by NickelBuckle9 · Oct 27, 2012 at 10:34 PM

Ok, so I think I figured it out... in case anyone else is interested, here's what I did.

I put a collider around my character, and added a rigid body to it, as needed by the OnCollisionEnter() script. Using OnCollisionEnter, I have it calculate the normal direction of the object with which it collides, and set a hit boolean to true false to see if contact was made. Then, I use the x and z components of that normal vector in the translation of my character when the collision is true.

It works relatively just like I wanted it to, but I have to divide the components by 1000 so that it doesn't jump back and forth rapidly as you try to enter the wall.

Code in C#:

CollisionDetect script:

 using UnityEngine;
 using System.Collections;
 
 
 
 public class CollisionDetect : MonoBehaviour {
     
     public static bool Hit = false;
     public static Vector3 Wall = new Vector3(0,0,0);
     
     public void Awake () {
 
     Wall = new Vector3(0,0,0);
     Hit = false;
         
     }
     
     void OnCollisionEnter(Collision collision) {
         
         ContactPoint contact = collision.contacts[0];
         Wall = contact.normal;
         Hit = true;
         
     }
     
     void OnCollisionExit(Collision collisionInfo) {
         Hit = false;
         
     }
     
 }




WASDmovement Script:

 using UnityEngine;
 using System.Collections;
 
 public class WASDmovement : MonoBehaviour {
     
     public float speed = 10f;
     public float rotate = 90f;
     
     void Update() {
         
         if(CollisionDetect.Hit == true){
                 
                 transform.Translate ((CollisionDetect.Wall.x)/1000,0,(CollisionDetect.Wall.z)/1000, Space.World);
                 float y = rotate * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
                 Debug.Log(CollisionDetect.Wall);
                 transform.Rotate(0, y, 0);        
         }
         
         
         else{
     
         float moveForward = speed * Time.smoothDeltaTime * Input.GetAxis("Vertical");
         float y = rotate * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
         transform.Translate (0, 0, 1 * moveForward);        
         transform.Rotate(0, y, 0);
             
         }
         
     }
 }
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

11 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

Related Questions

Button to change layers 1 Answer

Object collision triggering null reference at runtime when colliding with clone of itself after being childed. 1 Answer

Get objects in collision 1 Answer

Multiple Cars not working 1 Answer

OnCollisionEnter2D not being called on entering new tile once already called. 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