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.
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;
}
}
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;
I did this code in Dead (), but why gameObject goes back to "where it was dead" positoin after "respawning" in startPosition?)
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;
}
}
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;
}
}