Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Scrubbzi · Feb 01, 2017 at 09:10 AM · transformtrigger

How can i make my object go back to its start position?

I'm a new beginner at c#, I'm wondering how I can make an object basically "teleport" or go back to a different location after it hits a trigger? basically, I have a rock that is moving towards a target, but before it gets there, it goes through my "trigger" but I want my rock to go back to it's starting position after it hits it.

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 hassancortex · Sep 13, 2020 at 06:48 AM 0
Share

I had the same issue. @Scrubbzi Thank you @Chubzdoomer I was able to fix $$anonymous$$e with this code - note that, I am using a VR controller hence the 'OnDetachedFromHand' otherwise, everything else should work well. First, you get the position of the object, You get the rotation of the object, Upon detach from hand, you send the object back to those positions. I hope this is helpful.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Valve.VR.InteractionSystem;
 
 public class SnapToPosition : $$anonymous$$onoBehaviour
 {
     private Vector3 snapPosition;
     private Quaternion originalRotationValue;
     float rotationResetSpeed = 1.0f;
 
     private void Start()
     {
         snapPosition = this.transform.position;
         originalRotationValue = transform.rotation;
     }
 
     private void OnDetachedFromHand(Hand hand)
     {
         this.transform.position = snapPosition;
         transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * rotationResetSpeed); 
         GetComponent<Rigidbody>().isKinematic = true;
     }
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Chubzdoomer · Feb 01, 2017 at 08:48 PM

Put this at the top of your object's script:

 Vector3 startPos;

Then somewhere inside your object's Start or Awake function:

 startPos = this.transform.position;

Then whenever you want your object to go back, you can just do this:

 this.transform.position = startPos;

Comment
Add comment · Show 1 · 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 RenovatoCreations · Sep 05, 2019 at 04:42 AM 0
Share

I did this code in Dead (), but why gameObject goes back to "where it was dead" positoin after "respawning" in startPosition?)

avatar image
0

Answer by hassancortex · Sep 16, 2020 at 07:56 PM

I had the same issue. @Scrubbzi Thank you @Chubzdoomer I was able to fix mine with this code - note that, I am using a VR controller hence the 'OnDetachedFromHand' otherwise, everything else should work well. First, you get the position of the object, You get the rotation of the object, Upon detach from hand, you send the object back to those positions. I hope this is helpful.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Valve.VR.InteractionSystem;
 
 public class SnapToPosition : MonoBehaviour
 {
     private Vector3 snapPosition;
     private Quaternion originalRotationValue;
     float rotationResetSpeed = 1.0f;
 
     private void Start()
     {
         snapPosition = this.transform.position;
         originalRotationValue = transform.rotation;
     }
 
     private void OnDetachedFromHand(Hand hand)
     {
         this.transform.position = snapPosition;
         transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * rotationResetSpeed); 
         GetComponent<Rigidbody>().isKinematic = true;
     }
 }
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 hassancortex · Sep 13, 2020 at 06:48 AM

I had the same issue. @Scrubbzi Thank you @Chubzdoomer I was able to fix mine with this code - note that, I am using a VR controller hence the 'OnDetachedFromHand' otherwise, everything else should work well. First, you get the position of the object, You get the rotation of the object, Upon detach from hand, you send the object back to those positions. I hope this is helpful.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Valve.VR.InteractionSystem;
 
 public class SnapToPosition : MonoBehaviour
 {
     private Vector3 snapPosition;
     private Quaternion originalRotationValue;
     float rotationResetSpeed = 1.0f;
 
     private void Start()
     {
         snapPosition = this.transform.position;
         originalRotationValue = transform.rotation;
     }
 
     private void OnDetachedFromHand(Hand hand)
     {
         this.transform.position = snapPosition;
         transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * rotationResetSpeed); 
         GetComponent<Rigidbody>().isKinematic = true;
     }
 }
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform.find Does not work 0 Answers

Player becomes invisible after transform.position change,I cant see my Player after transform.position change. 0 Answers

How to turn off isTrigger when on Entering and turn on isTrigger on Exiting. 1 Answer

Object moves when game starts, but shouldn't until player collides with trigger 0 Answers

Resetting my cannon fire to its original transform 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