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 hettex · Jun 13, 2012 at 11:19 PM · javascripttransformpositionloop

Getting an Objects position once

Hello,

I am having an issue in getting a value once. Basically I am trying to move a platform back and forth a certain distance. The plat form does not start at x = 0, therefore if I place the object at x = 30, and I ask it to go +20x and return to 0 it will end up travelling from 50x back to 0. I have tried to make it so I ask it to go 20 + transform.position.x, but that ends up making it go on forever. I know I am bad at explaining things so here is the code I am using so far, any help is much appreciated. Thanks.

 var elevatorDirection : Vector3 = Vector3.left;
 var height; 
 var Distance = 10;
 var speed = 5;
 height = Distance + transform.position.x;
 
 
 function Update(){
     transform.position -= elevatorDirection * Time.deltaTime * speed;
 
     if(transform.position.x < height) {
         elevatorDirection = Vector3.left;
     }
 
     if(transform.position.x >= height) {
         elevatorDirection = Vector3.right;
     }
 }
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
0

Answer by aldonaletto · Jun 13, 2012 at 11:26 PM

You should save the initial position at Start, and base all movements on it:

var elevatorDirection : Vector3 = Vector3.left; var height: float; // specify the variable type when there's no initial value var Distance = 10; var speed = 5; var pos0: float; // holds the initial X coordinate

function Start(){ pos0 = transform.position.x; // register the initial position in pos0 }

// always specify the distance added to pos0:

height = Distance + pos0;

function Update(){ transform.position -= elevatorDirection Time.deltaTime speed;

 if(transform.position.x < height) {
     elevatorDirection = Vector3.left;
 }

 if(transform.position.x >= height) {
     elevatorDirection = Vector3.right;
 }

} But there's a better way to do what you want:

var elevatorDirection : Vector3 = Vector3.left; var Distance = 10; var speed = 5; var dest: Vector3; // holds the desired position var pos0: Vector3; // holds the initial position

function Start(){ pos0 = transform.position; // register the initial position in pos0 }

// always specify the destination relative to pos0:

dest = pos0 + Distance * Vector3.right;

function Update(){ transform.position = Vector3.MoveTowards(transform.position, dest, Time.deltaTime * speed); } MoveTowards takes care of determining to which direction move in order to reach the destination (the 2nd parameter), and never passes beyond it.

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 Fehr · Jun 13, 2012 at 11:32 PM

Try this:

 function Update(){
     transform.position += elevatorDirection * Time.deltaTime * speed;
 
     if(transform.position.x > height) {
         elevatorDirection = Vector3.left;
     }else if(transform.position.x < 0){
         elevatorDirection = Vector3.right;
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Vector3 Transform.Position Not Working 2 Answers

Position of a GameObject 2 Answers

Change object position on trigger enter 3 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