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 ReedMon · Nov 13, 2012 at 04:23 AM · colliderpositionobjectmove

Script to move an objects position and rotation upon collision with player?

How to implement a script to move an objects position once the player hits the box collider. The way im using this is there is a mask on the ground and the player notices it but there is a room up ahead with a door. Once he's done searching that room up ahead he leaves the room and all of a sudden the mask appears infront of the door and a sound plays. How would I implement this? Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Nov 13, 2012 at 01:06 PM

OnCollisionEnter(collider other)

is the function that you use to deal with collisions.

you attach a script to the gameobject with the box collider.

other is going to be whatever other collider you collided with. it varies depending on what hit the collider, so first your going to want to check to see if what hit the box was the player.

if(other.tag == "Player")

or something like that.

tags are basically just things added to a gameobject that you add yourself but help you find it.

you might tag enemies with the word "Enemy" and you might tag certain special enemies with "Special Enemy" so you treat them different that normal enemies.

now that you know its the player you'll want to search the scene for the book.

findobjectinscene(gameobject book)

is the function you'll want to use.

lastly you'll want to use the transform component of the book to modify its position and rotation.

book.transform.position controls its position. you'll want to set it to be a little in front of the camera.

you can use

Camera.main.ViewportToWorldPoint(new vector3(.5f,.5f,depth))

.5f, .5f will give you the exact middle of the screen. depth is how far forward from the camera you want the object to be. i'd suggest maybe 2? change it up and find out.

you'll need to rotate the object as well. you can simply make it look at the player or the main camera book.transform.rotation.LookAt(Camera.main)

you can play a sound attached to the book.

book.audio.play()

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

10 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

Related Questions

changing pivot's placement 2 Answers

The appearance of some prefab appears somewhere else but their collider stays in the good place, that to make? 0 Answers

What's the best way to check if an area is empty? 7 Answers

How to move an object from one position to desired position? 0 Answers

Move one object to anothers position over time. 2 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