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 shahinexir · Aug 14, 2012 at 07:48 PM · 2drotationgameobjectmovemoving

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 ;)

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 Mander · Aug 17, 2012 at 04:26 PM 0
Share

u can make a loop animation too.

there are alot of tuts online

https://www.youtube.com/watch?v=LbZTgvCE-8I

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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 shahinexir · Aug 17, 2012 at 03:20 PM 0
Share

would u pls give me an example with coding ? i really need this. tnx

avatar image
0

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

Comment
Add comment · Show 2 · 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 shahinexir · Aug 15, 2012 at 06:43 PM 0
Share

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 );

}

}

avatar image poncho · Aug 15, 2012 at 08:59 PM 0
Share

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; } }

avatar image
0

Answer by samljer · Apr 30, 2015 at 09:37 AM

Vector3.MoveTowards

Use that in a transform.translate() call for perfect raycasted movement.

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

12 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

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


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