- Home /
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
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,
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!
That's absolutely perfect! Exactly, what i was searching for! Thank you so much! =)
This is great but anything using this script goes right through objects. Can it be made to obey physics?
@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.
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 :)
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 ()
{
}
}
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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