• 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
2
Question by Msurdej · Nov 05, 2012 at 01:33 AM · c#positionreset

Resetting a position after reaching a certain point

I'm currently working on a game that has moving cars moving through my stage. I want to be able to return to their original positions once they reach a certain point, but the code I'm using doesn't seem to work. Relatively new to C#, any help would be appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class CarmovingX : MonoBehaviour {
     
     public int maxXPosition = 50;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
       transform.Translate(Vector3.up * Time.deltaTime);
         
         if( gameObject.transform.position.x >= maxXPosition)
         {
             transform.Translate(Vector3(12,y,z));
         }
         
     }
 }


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

2 Replies

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

Answer by Heisenbug · Nov 05, 2012 at 01:24 PM

I suggest you to store original position and orientation of your GameObject. When you decide to restore them, simply assign the stored value to its Transform. An example:

 public class CarmovingX : MonoBehaviour {
 
     public int maxXPosition = 50;
     private Vector3 initialPos;
     private Quaternion initialRotation;
 
     // Use this for initialization
     void Start () {
         initialPos = transform.position;
         initialRotation = transform.rotation;
     }
 
     // Update is called once per frame
     void Update () {
       transform.Translate(Vector3.up * Time.deltaTime);
 
        if( gameObject.transform.position.x >= maxXPosition)
        {
             transform.rotation = initialRotation;
             transform.position = initialPosition;
        }
 
     }
 }

If your CarmovingX GameObject is child of another GameObject, you may consider using Transform.localPosition and Transform.localRotation instead.

Comment
Add comment · Show 4 · 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 Msurdej · Nov 05, 2012 at 07:06 PM 0
Share

Works like a charm, Thanks!

avatar image RadhaUnity3d · Dec 24, 2013 at 12:32 PM 0
Share

transform.position = initialPosition it wrong declaration transform.position = initialPos it right declaration please update this if nt user may confuse .......

avatar image jijieciprian · Feb 12, 2016 at 08:36 AM 0
Share

works, Thanks!

avatar image robbiidegroot · Aug 05, 2021 at 10:34 AM 0
Share

For some reason my gameObject became very jittery. However, the example worked perfectly once I removed the following line.

 transform.Translate(Vector3.up * Time.deltaTime);

avatar image
0

Answer by JBaBe · Aug 04, 2016 at 12:54 PM

I used this script for my own project, i just had to replace the X axis by a Y axis to fit my needs. works well but i would like to only reset to initial position on the Y and Z axis how can I stop the script form reseting the gameobject's position along the X axis ?

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 itssethc · Mar 29, 2017 at 04:12 AM 0
Share

Did you ever find a fix for this? Have a similar issue.

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How can I reset the y position of a gameobject to a certain value each frame? 1 Answer

Multiple Cars not working 1 Answer

The Script keeps executing..instead executing onec 0 Answers

AI Patrolling 1 Answer


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