• 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 Puch · Jan 03, 2011 at 03:33 PM · javascriptmove

Up and down movement via Javascript

i'm designing a small game in which i need a puzzle piece to go up and down, but i can't seem to get it to work. here is the code i'm using.

var toplimit : float = 31; var botlimit : float = -31;

var goUp : float = 300;

var goDown : float = -300;

function Update () {

rigidbody.AddForce(Vector3(0,goUp,0));

if (transform.position.y < toplimit) rigidbody.AddForce(Vector3(0,goDown,0));

if (transform.position.y > botlimit) rigidbody.AddForce(Vector3(0,goUp,0)); }

can anyone see an issue? or think of a better way to do it? :)

thanks for any help

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

Answer by Ejlersen · Jan 03, 2011 at 05:28 PM

Try this:

var toplimit : float = 31; var botlimit : float = -31; var goUp : float = 300; var goDown : float = -300;

function Awake() { rigidbody.AddForce(Vector3(0, goUp, 0)); }

function Update() { if (transform.position.y > toplimit) { rigidbody.velocity = Vector3.zero; rigidbody.AddForce(Vector3(0, goDown, 0)); } if (transform.position.y < botlimit) { rigidbody.velocity = Vector3.zero; rigidbody.AddForce(Vector3(0, goUp, 0)); } }

Comment
Add comment · Show 3 · 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 Puch · Jan 03, 2011 at 10:19 PM 0
Share

thanks so much, this worked for me!

avatar image Ejlersen · Jan 04, 2011 at 07:10 AM 0
Share

No problem. Just remember to mark it as answered, such that people know ;-)

avatar image Deathsummer · Feb 25, 2015 at 09:00 AM 0
Share

thanks!!!! worked for me :D

avatar image
1

Answer by poncho · Jan 03, 2011 at 06:08 PM

well if i get right is a puzzle game, this means that the objects dont need to use the gravity or other physics attributes, if i guess right i propose you to fix the position

i'll write an example to make it go up and down between this points

var toplimit : float = 31; var botlimit : float = -31;

bool goingup = false;

function Update() { //if the position goes over the top limit then the goingup is false, meaning is not going up, then is going down if(transform.position.y+1>toplimit) goingup=false; //if the position goes below the bottom limit then the goingup is true, meaning is going up from now on if(transform.position.y-1<botlimit) goingup=true;

//going up... is self descriptive if(goingup) {//if is true then it goes 1 position up transform.position.y +=1; } else {//if is false then it goes 1 position down transform.position.y -=1; } }

for puzzle games, is easier to fix the position hope this is the answer you looking for =)

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 Puch · Jan 03, 2011 at 10:20 PM 0
Share

thank you for this answer too!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Move from point A to point B, then destroy? ( java S) 2 Answers

Move empty object random 1 Answer

how to stop every thing and enter into the OnTriggerEnter function? 0 Answers

How to move an object around with the arrow keys (Javascript) 1 Answer

Mouse Click to keyCode 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