- Home /
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);
}
Please format ALL of your code above. The first portion is not.
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);
}
Okay. Thank you robertbu. I'm going to try the code right away.
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?
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
Follow this Question
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