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
1
Question by derrtyones · Sep 20, 2012 at 09:25 PM · rotationjavascripttransformposition

Change object position on trigger enter

Hi,

I have a door which can open and close using a drag and rigidbody script. Sometimes the door gets stuck in a wall and it's a pain to get this fixed as this is most likely a physics bug.

What I'm trying to achieve is that once the door enters the wall (wall is collider), it must change to a specific position.

Basically my script saves the current X,Y,Z position/rotation. Once the player opens the door and the door gets stuck in the wall, it must get reset to the original position.

I tried to do with my following script, but I also get the following error. Can someone help me out fixing this issue? Resetting the position works, but how do I reset the rotation?

Error: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'. This is about the last line in the script that starts with "transform.rotation" in the onTriggerEnter function.

Script:

 var doorPosX : float;
 var doorPosY : float;
 var doorPosZ : float;
 var doorRotX : float;
 var doorRotY : float;
 var doorRotZ : float;
 
 function Start() {
     //store all current position and rotation for the door
     doorPosX = transform.position.x;
     doorPosY = transform.position.y;
     doorPosZ = transform.position.z;
     doorRotX = transform.rotation.x;
     doorRotY = transform.rotation.y;
     doorRotZ = transform.rotation.z;
 }
 
 function OnTriggerEnter (door : Collider) {
     if (door.gameObject.tag == "door") {
         Debug.Log("Reset");
         //if door gets stuck in wall, teleport door back into original position.
         transform.position = Vector3(doorPosX,doorPosY,doorPosZ);
         transform.rotation = Vector3(doorRotX,doorRotY,doorRotZ);
     }
 }
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

3 Replies

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

Answer by FlyingOstriche · Sep 20, 2012 at 09:42 PM

It would help if we knew what line it was.

for starters

 transform.position.x = Vector3(doorPosX,doorPosY,doorPosZ);

should be

 transform.position = Vector3(doorPosX,doorPosY,doorPosZ);
Comment
Add comment · Show 2 · 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 derrtyones · Sep 20, 2012 at 09:50 PM 0
Share

Thank you, that was a silly mistake. I updated the script in the original post. I get a new error which is also written in the same post. Right now the position is reset, but now how do I reset the rotation?

avatar image derrtyones · Sep 20, 2012 at 10:28 PM 0
Share

I got the rotation to work now too with the help of someone elses question @ http://answers.unity3d.com/questions/26984/reset-rotation-or-store-rotation.html.

avatar image
1

Answer by Bunny83 · Sep 20, 2012 at 10:45 PM

The two other answer are "mostly" correct but usually it's more convenient to use a Vector3 variable to store the initial position. For the rotation it's a bit tricky ;) the rotation property is a Quaternion. So either store the rotation as quaternion or use eulerAngles.

A Quaternion(wikipedia) doesn't work with angles in degree. It has 4 components (not just 3) which represents a rotation axis and a rotation around this axis. You usually don't mess around with the internal components of a quaternion unless you know how they work.

So do something like:

 var doorPos : Vector3;
 var doorRot : Quaternion;
 
 function Start()
 {
     doorPos = transform.position;
     doorRot = transform.rotation;
 }
 
 function OnTriggerEnter (door : Collider)
 {
     if (door.gameObject.tag == "door")
     {
        transform.position = doorPos;
        transform.rotation = doorRot;
     }
 }


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 bzgeb · Sep 20, 2012 at 10:36 PM

The problem is that you're trying to assign a Vector3 into position.x, which is a single float. Instead what you want to do is assign your Vector3 into transform.position, which is also a Vector3.

 transform.position = Vector3(doorPosX,doorPosY,doorPosZ);
Comment
Add comment · Show 2 · 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 Bunny83 · Sep 20, 2012 at 10:47 PM 1
Share

First you have a typo, you assign two times to transform.position ;) Second, rotation is not a Vector3.

avatar image bzgeb · Sep 20, 2012 at 11:33 PM 0
Share

Thanks. I wasn't thinking when I wrote this.

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

12 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

Related Questions

Following another object's position/rotation like parent/child relationship? 4 Answers

Smooth Rotation Help 1 Answer

transform position y changes when character rotates 0 Answers

How to predict position of object before transform.translate will apply? 1 Answer

How Can I Stop Rotation From This Script? 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