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 GamerBot · Mar 18, 2014 at 10:25 PM · javascriptmovementgame2d-platformerenemy

How can I set my cube moving left and right with custom switch?

Hello. I have been trying to write a script that can make the cube(an enemy)in my platform game to start moving from left and right. The script is in JavaScript. I only manage to make the cube move left, but only moving left. While the game is played I click on the switch in the inspector and then the cube starts moving to the right. How can I tell the cube to move which way to go using the script?


 var DestinationSpot:Transform;
 var OriginSpot:Transform;
 var Speed:float;
 var Switch:boolean = false;
     // For these 2 if statements, it's checking the position of the enemy.
     // If it's at the destination spot, it sets Switch to true.
     if(transform.position == DestinationSpot.position)
     {
         Switch = true;
     }
     if(transform.position == OriginSpot.position)
     {
         Switch = false;
     }
      
     // If Switch becomes true, it tells the enemy to move to its Origin.
     if(Switch)
     {
         transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
     }
     else
     {
         // If Switch is false, it tells the enemy to move to the destination.
         transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
     }
Comment
Add comment · Show 1
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 Nanobrain · Mar 18, 2014 at 10:36 PM 0
Share

Please format ALL of your code above. The first portion is not.

1 Reply

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

Answer by robertbu · Mar 19, 2014 at 03:54 AM

Your big problem here, is that you need to put your code inside an Update() function so that it gets called once per frame. Right now it only gets executed once. In addition, you should use 'speed * Time.deltaTime' as the last parameter in your MoveTowards() so that the code is frame-rate independent:

 function Update() {
     // For these 2 if statements, it's checking the position of the enemy.
     // If it's at the destination spot, it sets Switch to true.
     if(transform.position == DestinationSpot.position)
     {
         Switch = true;
     }
     if(transform.position == OriginSpot.position)
     {
         Switch = false;
     }
  
     // If Switch becomes true, it tells the enemy to move to its Origin.
     if(Switch)
     {
         transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed * Time.deltaTime);
     }
     else
     {
         // If Switch is false, it tells the enemy to move to the destination.
         transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed * Time.deltaTime);
     }
  }

Note when you have something that oscillates, consider using Mathf.PingPong() or Mathf.Sin() to set the position. Example:

 #pragma strict
 
 var dest : Transform;
 var org : Transform;
 var speed : float = 1.5;
 
 function Update() {
     var t = Mathf.PingPong(Time.time * speed, 1.0);
     transform.position = Vector3.Lerp(dest.position, org.position, t);
  }

 
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 GamerBot · Mar 20, 2014 at 05:45 PM 0
Share

Okay. Thank you robertbu. I'm going to try the code right away.

avatar image GamerBot · Mar 20, 2014 at 05:56 PM 0
Share

IT WOR$$anonymous$$S!! Thank you robertbu for helping me out. I have one question for you: How can I give you a "Thumps Up" in this website?

avatar image robertbu · Mar 20, 2014 at 06:41 PM 0
Share

You need 15 $$anonymous$$arma points before you can thumbs up/down (which you just now do). You can click on the thumb up icon on any question, answer, or comment.

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

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

Related Questions

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

(2d in 3d) Movement relative to camera using JS 0 Answers

2D enemy AI 1 Answer

How to make an enemy pace? 1 Answer

Enemy rotation issues - Javascript 0 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