• 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
1
Question by Bumbob · Aug 04, 2014 at 04:20 AM · javascriptcollisionvector3functiontransform.position

How would I move an Object after collision?

I have my game set up in 2D mode, There is a side scrolling camera with a bar that follows it, during the game bricks will move across the screen, when they disappear off screen I am trying to get them to collide with the bar following and the move back by an x value of Random.Range(-6,-8) I am unaware if my code is wrong, or if I have an error, but I will post what code I have used.

     function OnCollisionEnter(collision:Collision){
     collision.transform.position = Vector3(Random.Range(-6,-8), Random.Range(-3,6),0);  
      }
 
Comment
Add comment · Show 1
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 tyrike thompson · Aug 04, 2014 at 04:42 AM 0
Share

What does the error say?

1 Reply

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

Answer by Noob_Vulcan · Aug 04, 2014 at 04:41 AM

If your bar collider is moving right then Just Make Sure that

  1. Your bricks/Bar has RigidBody. Because Collision is detected by Physics. So you will have to make one of them rigidbody.

  2. If you have Followed Step 1 and still your code is not working then you have problem in bar position. Just make the Bar child of camera.

I hope this should resolve your problem.

Comment
Add comment · Show 3 · 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 Bumbob · Aug 04, 2014 at 04:45 AM 0
Share

as I was thinking about it more, is that going to always move the bricks to that set random range, or will it continue pushing them back, my goal is to make them get pushed back every time they are collided with.

avatar image Noob_Vulcan · Aug 04, 2014 at 05:04 AM 0
Share

can u tell me what exactly u want to achieve so that i can help u.

avatar image Bumbob · Aug 07, 2014 at 10:32 PM 0
Share

I figured it out, in case anyone else needs the code, here it is

 function OnCollisionEnter (col : Collision)
 {
     if(col.gameObject.name == "WallL")
     {
         transform.position = Vector3(transform.position.x - Random.Range(-16,-20), Random.Range(-3,6),0);        
       }

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

Script error: respawn upon collission 2 Answers

Vector3 of moving object always returns (0,0,0) 1 Answer

Collision Detection in Capsule Collider 0 Answers

Move empty object random 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges