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
3
Question by Lena · Dec 14, 2012 at 10:54 AM · movelinecurve

How to move an enemy on a curvy line?

Hey guys,

first, sorry for my bad english, i'm german.

I want an enemy to move through the world on a line. But not straight, it should have a lightly curve in it, just like you were on an arc of a really big circle or something like that.

Any ideas? Would be great!

Thanks, Lena

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

4 Replies

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

Answer by save · Dec 14, 2012 at 11:21 AM

There are a couple of ways to accomplish this. Creating an animation is one visual way to do it where moving objects along a bezier curve is trivial. Although this might not be a good case if your objects should behave differently depending on certain circumstances.

Through code you would setup a couple of variables which determines the start position, end position, the bending over time and a time frame. This might sound much more complex than it actually is, here's an example:

 var startPosition : Vector3 = Vector3(-10,0,0);    //The starting position in world space
 var endPosition : Vector3 = Vector3(10,0,0);    //The ending position in world space
 var bending : Vector3 = Vector3.up;                //Bend factor (on all axes)
 var timeToTravel : float = 10.0;                //The total time it takes to move from start- to end position
 
 function Start () {
     MoveToPosition();
 }
 
 function MoveToPosition () {
     var timeStamp : float = Time.time;
     while (Time.time<timeStamp+timeToTravel) {
         var currentPos : Vector3 = Vector3.Lerp(startPosition, endPosition, (Time.time - timeStamp)/timeToTravel);
         
         currentPos.x += bending.x*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
         currentPos.y += bending.y*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
         currentPos.z += bending.z*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
         
         transform.position = currentPos;
         
         yield;
     }
 }

To visually describe this better, Arc over time

What the code does is to move the transform over time in an arc. If you want your objects to face a certain direction meanwhile moving on this path, you would most easily create a parent and put this script onto it, then move/rotate the child to which ever direction you please. Feel free to ask if you have any questions!

Comment
Add comment · Show 10 · 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 Lena · Jan 02, 2013 at 02:22 PM 0
Share

That's absolutely perfect! Exactly, what i was searching for! Thank you so much! =)

avatar image save · Jan 02, 2013 at 08:32 PM 0
Share

Great! Good luck!

avatar image UNDERHILL · Jul 10, 2014 at 08:09 PM 0
Share

This is great but anything using this script goes right through objects. Can it be made to obey physics?

avatar image Chandler221 · Jul 29, 2014 at 07:20 AM 0
Share

good code.thanks.helped me as well.

avatar image save · Aug 15, 2014 at 01:32 PM 0
Share

@UNDERHILL The physics engine cannot and should not work together with transform translations. You could Raycast in the direction the object is moving, use a Physics Overlapshere, otherwise you would have to write a velocity script which will add forces onto your rigidbody in a similar manner of how currentPos gets updated in the while loop.

Show more comments
avatar image
1

Answer by skoandi · Dec 14, 2012 at 11:56 AM

http://pixelplacement.com/2010/12/03/visual-editor-for-itween-motion-paths/

This is a great plugin :)

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 TornschuhJette · Oct 24, 2015 at 04:28 PM

Hey there!had the same Problem.Get the aboveementionend Plugin ItweenPath and use this Patrol Script to make your enemy follow with animation,rotationdamping and speed.

don´t forget to properly name your curve and put it in the patrol script. This way you can have different enemys following different curves.

 using UnityEngine;
 using System.Collections;
 
 public class PatrolScript : MonoBehaviour {
 
     public string pathName;
     public double speed = 10;
     public iTween.LoopType loopType = iTween.LoopType.loop;
     public iTween.EaseType easeType = iTween.EaseType.linear;
     public bool isLocal = true;
     public bool orientToPath = true;
     public double lookAhead = 0.3;
     public int delay = 0;
     public bool moveToPath = false;
 
 
     // Use this for initialization
     void Start () {
         iTween.MoveFrom(gameObject, iTween.Hash ("path" ,iTweenPath.GetPath(pathName),
                                                  "speed", speed, 
                                                  "looptype", loopType, 
                                                  "easetype", easeType, 
                                                  "orienttopath", orientToPath, 
                                                  "islocal", isLocal, 
                                                  "lookahead", lookAhead, 
                                                  "delay", delay ,
                                                  "movetopath", moveToPath
                                                  )
                         );
 
     }
     
     // Update is called once per frame
     void Update () 
     {
 
 
     
     }
 }
 

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 shomeser · Mar 02, 2016 at 01:17 PM

I faced with almost the same task. My objects were located in random positions, the line between two of them was not parallel to axis. So, I created common solution which can helps anybody.

Here is demo video, source code on Github.

The script is very simple by itself. All we need to do is calculate two vectors - vector of main direction and orthogonal vector. After this we need to subordinate orthogonal vector by sine function, and then perform addition of these two vectors, so the sum will move object following exact sine curve.

 void FixedUpdate () {
     float t = Time.time - startTime;
     rb.velocity = direction * speed + orthogonal * amplitude * Mathf.Sin (frequency * t);
 }


You can find more details here - how to setup scene, components, etc.

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

17 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

Related Questions

Calculate needed resolution to draw curve from line segments in editor 1 Answer

How can i move a Keyframe on an Animation Curve with overwriting the key on the target time? 1 Answer

Creating a faded out curved line 0 Answers

Can iTween be used to constrain motion for an arbitrarily drawn curve? 1 Answer

How to move objects along a curve? 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