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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by KG3 · Jun 16, 2013 at 02:06 PM · collisiongameobjectboxcollider

Empty/Game Object Gets Stuck inside after Colliding with another Object

Hey Unity 3D Community, I have a little newbie problem. I am creating a space invaders video game. The Current Project looks like this: alt text

In the game there is a empty object that is moving along the X-axis, when the empty object hits the left wall the direction will change to 1 and it will move right, when it then hits the right wall, the direction will change to -1, and so forth. My problem is doing the same thing with the user, when I hit against a wall.

Here is the Code, for the moving empty object:

 using UnityEngine;
 using System.Collections;
 
 public class enemyspawn_script : MonoBehaviour {
     int direction = 1;
     int speed = 1;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         transform.Translate(new Vector3(direction * speed * Time.deltaTime, 0, 0));
     
     }
 
     void OnCollisionEnter(Collision col)
     {
         Debug.Log("hit");
         if (col.gameObject.tag == "Left")
         {
             direction = (-1);
         }
         if (col.gameObject.tag == "Right")
         {
             direction = 1;
         }
     }
 
   
 }

But when the empty game object collides, with a wall this happens: alt text

The object gets stuck, in the wall a little bit. I need help to figure our how to make the object bounce off smoothly against a wall, or bumper, or object.

example1.jpg (22.8 kB)
example2.jpg (31.0 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Owen-Reynolds · Jun 16, 2013 at 03:27 PM

The quickest way is to grow the wall colliders a little bit, or grow the enemy collider a little, or shrink the wall mesh a little. The exact same overlap will happen, but it will look better.

The "real" problem is that your code manually moves the object like an Inspector drag. That's fine, but it happily lets you drag through solid objects. Suppose you completely redid the movement to use rigidybody.velocity (or AddForce, etc...) and let the system do the moving. Then the frame it was going to sink into the wall, the system would "know" it counts as a real moving object, so would bounce.

But there will almost always be a tiny bit of overlap.

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 KG3 · Jun 16, 2013 at 03:19 PM

It turns out that I am a idiot.

I tagged the Right Wall "left" and the Left Wall "Right", so when The game object hit The right wall it was reading in Left.

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

15 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

Related Questions

collisions in character not working! 2 Answers

Simple two cube collision is not good? 0 Answers

Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers

Detect cloned object detection once 0 Answers

How can I make my character "die"? 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