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 Earth-O-Matic · Mar 18, 2011 at 07:34 AM · collisiondirectiononcollisionenter

Change Direction on Collision

So I've been killing myself over this trying to get it to work and I've finally decided to cave and post here. I'm an artist so I'm trying to learn to script. I'm trying to make an object change direction once it has hit another object. Right now its just a cube moving form one side of the screen to the other. Once it hits the wall on the right I want it to change direction and again when it hits the left wall. Here is the code I have...

var isMoved = true; var change = false;

function Update () {

if (isMoved == true) transform.Translate(Vector3(1,0,0) * Time.deltaTime);

if (change == true) transform.Translate(Vector3(-1,0,0) * Time.deltaTime); }

function OnCollisionEnter (hit:Collision){ if (hit.gameObject.tag == "wallRight")

     {
     isMoved = false;
     change = true;
     Debug.Log("hit");
     }

 if (hit.gameObject.tag == "wallLeft")
     {
     change = false;
     isMoved = true;
     Debug.Log("hit");
     }

}

My problem is that at the start of the level the cube moves fine to the other side but once it hits the right wall nothing happens. I don't even get the "hit" message in the debug. So something is obviously not registering. Any help is really appreciated. Thank you!

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

6 Replies

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

Answer by Statement · Mar 20, 2011 at 08:08 PM

It seems you want to simply invert the x motion of the object when it hits a wall. We can do this by multiplying the x with -1. So for example if the object was moving with direction (4, 7, 1) and hitting one of the walls, it would change direction to (-4, 7, 1). This also means you could change to only use one tag: "xWall" or something. It doesn't matter if it is the left or right wall.

// This is our direction we're travelling in. var direction = Vector3(1, 0, 0);

// Move it along its direction. function Update () { transform.Translate(direction * Time.deltaTime); }

// If we hit a left or right wall, invert x direction. function OnCollisionEnter (hit : Collision) { var tag = hit.gameObject.tag;
if (tag == "wallRight" || tag == "wallLeft") direction.x *= -1; }

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
2
Best Answer

Answer by Earth-O-Matic · Mar 20, 2011 at 09:09 PM

Thanks to every one on this one! Finally got it working. This is what I ended up going with for any future people looking for a similar solution. I'm using triggers in this case because I eventually want to use them for other things.

// Direction of movement - X var direction = Vector3(1, 0, 0);

// Move it along its direction. function Update () { transform.Translate(direction * Time.deltaTime); }

// Hit edge of screen - invert x direction. function OnTriggerEnter(collisionInfo : Collider) {

var bump = collisionInfo.gameObject.tag; if( bump == "wallX"); direction.x *= -1; Debug.Log("hit"); }

Thanks Statement, I ended up using your cleaner code. It helped point out the error of my ways. Learning is fun! Thanks again guys!

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 Jake-L · Mar 18, 2011 at 08:59 AM

Hi Brian,

you need to set up Colliders on all objects that you want to collide. Have a look at this page, especially look at the collider matrix (down the page under "Advanced") to check if your objects meet all conditions to send the trigger/colliding messages.

In your case you could make the walls a Static Collider and the ball a Kinematic Rigidbody Trigger Collider and catch the collision in a OnTriggerEnter function.

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 Meltdown · Mar 18, 2011 at 09:34 AM

Take a look at adding RigidBody components to your cubes, and when they collide with something, look at using RigidBody.AddForce to send them in another direction.

If your colliders in your game are added to the physics system Unity can take care of all the collisions for you automatically instead of you having to worry about them yourself.

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 Earth-O-Matic · Mar 20, 2011 at 07:51 PM

So I have physics components on the main object using rigid body but using add force to change its direction gives me some unwanted results. I really want a single steady movement for the object. The idea is a character walking across the screen. As soon as he hits one side of the screen he turns around and same on the other side.

I got the direction to change when the cube hits the right wall now so its detecting my triggers. (ended up using triggers for the side walls) Here is the code I have now....

var isMoved = true; var change = false;

function Update () {

if (isMoved == true)

 transform.Translate(Vector3(1,0,0) * Time.deltaTime);

if (change == true)

 transform.Translate(Vector3(2,0,0) * Time.deltaTime); 

}

function OnTriggerEnter(collisionInfo : Collider){

if(collisionInfo.gameObject.tag == "wallRight")

 isMoved = false;
 change = true;
 Debug.Log("hit");


if(collisionInfo.gameObject.tag == "wallLeft")

     change = false;
     isMoved = true;
     Debug.Log("hit");


}

My problem now. is that it seems to run everything under the On Trigger enter function all at once. The player will hit the right wall. Turn around. Send the "hit" text to the command console but it will send both of them as if it also hit the left wall already. Then once it actually gets to the left wall it just passes right through it. What am I doing wrong here? :-/ Thanks again for all the help.

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
  • 1
  • 2
  • ›

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

No one has followed this question yet.

Related Questions

Direction isn't changing after multiplying it *-1 0 Answers

Change direction on collision problem 4 Answers

Simple Collision Detection not working. [Solved] 3 Answers

Collision Only being detected on one of the objects involved in the collision - C# 0 Answers

How do Unity manages collisions in a game internally? 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