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
0
Question by Chippers · Jul 11, 2012 at 02:32 AM · transformifupdownspike

Moving Object up and down without player input (such as a spike that kills you)

Hello. Currently I'm trying to move an object up and down with if statements on the y axis. The object is a spike that if the player touches dies.

This is the script I'm using. I want to have the spike move up to a certain point on the y axis and when it hits it to move down until it hits another point and then have it move up again. It's probably worth noting that i have the spike attached to an empty game object so its starting position is always 0.

 function Update() { 
 if (this.transform.position.y <= 0){
 this.transform.position.y = this.transform.position.y + (10*Time.deltaTime);
 }
 if (this.transform.position.y >= 10){
 this.transform.position.y = this.tranform.position.y - (5*Time.deltaTime);
 }
 }
 
 function OnTriggerEnter (other :Collider){
 Application.LoadLevel("Game Over");
 }

Right now this script will move the spike up 10 on the y axis but then it will stop there and not go back down. Any help would be greatly appreciated !

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
2

Answer by Muuskii · Jul 11, 2012 at 02:59 AM

Right now you have two if statements, one is only done when the spike is at the bottom of it's course and the other only at the top. You have nothing for in between. You need to do something more like:

 float velocity = 10;
 
 transform.position.y -= velocity * TimedeltaTime;
 
 if(transform.position.y >= 10)velocity = -5;
 else if(transform.position.y <= 0)velocity = 10;

You don't need the this keyword before transform and you need a way for the program to remember what it's doing between waypoints

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 VIPINSIRWANI · Sep 18, 2013 at 12:07 PM

 start = new Vector3(0,0,0);
  target= new Vector3(4,0,0);
 
 void Update () {
         Vector3 pos = transform.position;
         if(pos != target)
         {
             transform.position = Vector3.MoveTowards(pos,target,0.03f);
         }
         else
         {
              
              target = start;
             if(pos == start)
             {
                 target = new Vector3(4,0,0);
             }
         }
         
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

6 People are following this question.

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

Related Questions

Perpendicular Vector3 0 Answers

physics question my object setup failing collision 3 Answers

GameObject automatically moves up when upgrade is purchased. 2 Answers

Knowing when mouse has been released 2 Answers

GUI begin vertical, to go Up 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