- Home /
Move sprite from point A to point B
Hi, Pretty new to Unity and i'm trying to make a very simple iphone game. , I'd like to know how to move a sprite (2D) move from random point A to random Point B not just that, but i want it to change direction halfway through.
Thank You.
Your question is both fuzzing and has been covered many times. It is also covered in many basic tutorials. Please search out and try the many answers on now to move objects from one position to another. If you get stuck, posts back with your attempt.
The rotation is a separate issue. We try and keep Unity questions to a single issue. If you end up asking the rotation as its own question at some point, be specific. What two directions will the object be looking at? Does the rotation change over time? If it changes over time, is it a continuous change from the start to end movement, or a quicker rotation in the middle of the movement? Etc.
I already used google and youtube before asking here, some people used AddForce others used whole bunch of If statements. I really want something simple. As for the rotation, it's a 2d game so we only use (x,y) and i want it to happen only 1 time.
You could at least pinpoint me to a respectful tutorial or something. Someone else might have if it stayed opened.
You question comes across as a write-it-for-me question. You describe the specification for what you want but don't show that you've done any work on the problem. And you don't provide critical information for someone to give you an accurate answer.
$$anonymous$$ovement by Transform
$$anonymous$$ovement by Ridbody
$$anonymous$$ovement by CharacterController
Does the movement need support collisions
Nature of the rotation
Etc.
I just did a Google search for "unity3d move object between two points," and found half a dozen useful web pages on the first page of results (including the reference page for Vector3.Lerp()). If you'd come to the table with any sort of attempt to solve the problem on your own, I would have left it open.
here is my attempt:
public float speed;
public float $$anonymous$$Force;
public float maxForce;
public float counter;
private float counter2;
void Start ()
{
Push ();
}
void Update ()
{
counter2 -= Time.deltaTime;
if ( counter2 < 0)
{
Push ();
counter2 = counter;
}
}
void Push ()
{
float force = Random.Range($$anonymous$$Force, maxForce);
float x = Random.Range(-1f, 1f);
float y = Random.Range(-1f, 1f);
rigidbody2D.AddForce( force * new Vector2(x, y) * speed);
}
}
I'm not quit satisfied with using force, also tried Vector2.lerp but for some reason it just teleported ins$$anonymous$$d of smoothing. Also i want to make it randomly change position only once.
here is my Lerp attempt:
private Vector2 startPoint ;
public float speed ;
public float counter;
private float counter2;
void Start ()
{
startPoint = transform.position;
}
// Update is called once per frame
void Update ()
{
counter2 -= Time.deltaTime;
if ( counter2 < 0)
{
RandomPosition ();
counter2 = counter;
}
}
void RandomPosition ()
{
float counter = speed * Time.deltaTime;
float x = Random.Range(-50f, 50f);
float y = Random.Range(-100f, 100f);
Vector2 endPoint = new Vector2(x, y);
transform.position = Vector2.Lerp(transform.position, endPoint, Time.deltaTime * speed);
}
}
Your answer
Follow this Question
Related Questions
Replacing iTween.MoveUpdate with iTween.MoveTo 1 Answer
how do I sync movement with mouselook 1 Answer
Angry Birds hold, drag and catapult an object ... 2 Answers
iPhone Touch RayCastHit problem 2 Answers
Gyroscope based First person controller 0 Answers