- Home /
how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ???
hi . im making a 2d game and i have a sphere as an enemy . i want to move this sphere between 2 diffrent positions along the x axis just like old super mario enemies whom could move between 2 points . i mean first move to the left then when it gets to the (A) , move to the right direction to the (B) and also have rotation while moving to left and right . but i dont know what should i use , or what kind of function i can use to make this happen. or is there anything like that available in unity to make this ? need some guids and tips . tnx alot .
right now im using this code :
var enemySpeed : float;
var rotationSpeed : float;
function Update ()
{
amtToMove = enemySpeed * Time.deltaTime;
transform.Translate(Vector3.left * amtToMove);
enemyRotationSpeed = rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3(0,0,0.5) * enemyRotationSpeed , Space.World);
}
the code is working well . its just moving to the left but dont know how am i suppose to tell him to rotate and move right when u get to for example (A) POINT . another problem is that when its rotating it keeps going downward into the floor! i have a rigid body with is kinematic on my enemy object . tnx guys ;)
u can make a loop animation too.
there are alot of tuts online
Answer by sman_47o · Aug 14, 2012 at 09:43 PM
have two empty game objects with triggers and name one A and the other B. tell your enemy look at the point A with the lookAt function and set its velocity to make it move forward on entering the A trigger set the object to look at target B and vis versa.
also rotate it on the y axis, not the z axis, (or maybe i have this reversed in my head but i don't think so) or else it will move into the ground.
would u pls give me an example with coding ? i really need this. tnx
Answer by poncho · Aug 15, 2012 at 03:37 PM
you need to separate
for moving part you need a boolean to know if moving left or moving right
if(moving) { amtToMove = enemySpeed Time.deltaTime; if(movingleft) transform.Translate(Vector3.left amtToMove); if(movingright) transform.Translate(Vector3.right * amtToMove);
if(movingleft)// this is for moving left if(transform.position.x < targetApositionx)//if it reaches the A position then stop moving left, activate right moving but deactivate move to make the rotation first { movingleft = false; movingright = true; moving = false; rotating = true; } //same logic but to the right if(movingright) if(transform.position.x > targetBpositionx) { movingright = false; movingleft = true; moving = false; rotating = true; } }
if(rotating) { here you need to make something similar, define the rotation range, or how many degrees the object will rotate before moving again, after the rotation is complete, then activate moving and deactivate rotating }
please first you need to get the logic, then use the logic to javascript
hope this is of any help
tnx but im really confused! i tried what u said but something is wrong , then i changed it a lil bit but again it does not work . actually now it doesnt moves at all .
it takes lots of coding :( is there any better solution? this is my changes :
var enemySpeed : float;
var rotationSpeed : float;
var rotateleft : boolean;
var rotateright : boolean;
var movingright : boolean;
var movingleft : boolean;
function Update () {
if (movingleft)
{
if (transform.position.x < -11.1)
{
rotateleft = true;
amtTo$$anonymous$$ove = enemySpeed * Time.deltaTime;
transform.Translate(Vector3.left * amtTo$$anonymous$$ove,Space.World);
}
movingleft = false;
rotateleft = false;
movingright = true;
}
if (movingright)
{
if (transform.position.x > 2)
{
rotateright = true;
amtTo$$anonymous$$ove = enemySpeed * Time.deltaTime;
transform.Translate(Vector3.right * amtTo$$anonymous$$ove,Space.World);
}
movingright = false;
rotateright = false;
movingleft = true;
}
if (rotateleft) {
enemyRotationSpeed = rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3(0,0,8) * enemyRotationSpeed );
}
if (rotateright) { enemyRotationSpeed = rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3(0,0,-8) * enemyRotationSpeed );
}
}
watch your ifs they enter for the first time in the movingleft, you need to switch the moving values if the object gets to the side you want, i mean if you are moving left and your target is -10, you will switch to movingright if the actual position value is $$anonymous$$or to -10, then if moving to right you will switch if the actual value is more than 5
if(movingleft) { //move code first if(myposition.x < -10)// your left margin { //you stop moving left, then start moving right movingleft = false; movingright = true; } }
Answer by samljer · Apr 30, 2015 at 09:37 AM
Vector3.MoveTowards
Use that in a transform.translate() call for perfect raycasted movement.
Your answer
Follow this Question
Related Questions
HOW stop object rotation when a another is not moving 0 Answers
left hand invaders moving into each other 1 Answer
Enemy Rotation Problem 1 Answer
Move towards current direction on the Z axis (2D) 1 Answer
Vector3.RotateTowards Problem 1 Answer