- Home /
GameObject moving left to right AND up
Hi! I've got a rather simple question (I guess), but can't get it to work. I've got a GameObject which moves left to right on a Loop. It's an instatiated Object. I found the script here at UnityAnswers:
public class ghostMovement : MonoBehaviour {
public float delta = 0.5f; // Amount to move left and right from the start point
public float speed = 2.0f;
private Vector2 startPos;
void Start () {
startPos = transform.position;
Invoke ("GhostDeath", 1f);
}
void Update () {
Vector2 v = startPos;
v.x += delta * Mathf.Sin (Time.time * speed);
transform.position = v;
}
void GhostDeath(){
Destroy (gameObject);
}
}
This works great. But I can't get this object to move up at the same time. Adding a negative gravity in the rigidbody2D doesn't work. Adding something like v.y += Time.time * 0.5f; doesn't work either, the sprite doesn't start to go up at the exact Location and the object starts to appear way too far away from where it should as time goes on.
Cand someone give me an advice on how to solve this Problem? Thanks in advance!
I added v.y += Time.time * 0.5f
right below v.x += delta ...
and the object moves up (and from left to right ofcourse).
the object starts to appear way too far away from where it should as time goes on
I don't understand this. So the desired behavior is "move from left to right and vice versa and move up, destroy yourself after 1s". Right?
Sorry for not being specific enough. I'm working on a topdown shooter where your character can die and respawns after a short time. When the character dies I want this ghost prefab to appear on the location the character died. The ghost should move up and left to right. After 1 second the ghost disappears (the gameobject gets destroyed). Since you get respawned again I need the "new" ghost appearing again at the exact location where the player died again. With the solution you suggested the first ghost appears a bit too high above the players death location. The second ghost appears a bit more far away from the new death location and so on. I hope you understand what I mean.
Answer by StaNov · Aug 11, 2015 at 06:31 PM
The second ghost appears higher, because the time goes on. At the beginning, the time is zero, so your ghost moves up from y=0. But if you spawn another ghost later, the time is no more zero. It's something positive, like 20 (in seconds).
You can save time of the animation start when you spawn a ghost. And as time use difference between current time starting time.
private float startTime;
void Start () {
startPos = transform.position;
startTime = Time.time;
}
void Update () {
Vector2 currentPos = startPos;
float currentTime = Time.time - startTime;
currentPos.x += delta * Mathf.Sin (currentTime * speed);
currentPos.y += delta * currentTime * 0.5f;
transform.position = currentPos;
}
Thx for your reply. Unfortunately I'm nit able to check this out now, but I will test it tomorrow. Thanks a lot.
Your answer
Follow this Question
Related Questions
Change animation at runtime with Mecanim 0 Answers
Not able to fire projectiles at mouse position 2 Answers
Why time in instantiate object with this script doesn't work? 0 Answers
Bullets will instantiate but they won't move. 1 Answer
Instanitiate PreFabs and let them move to different locations 1 Answer