- Home /
Move character up and down while animation runs
Hello,
i got a question. I created a flappy bird clone with a tutorial but i want to add the death feature. When the player is death(collides), he has to move up a bit and then fall through the ground(like in mario).
I tried to use the code below but nothing happend. I think it is important to say that the death is an animation, because the bird turns from normal color into gray.
var pos = transform.position;
pos.y += 1;
Is there any possibility?
Answer by maccabbe · Mar 02, 2015 at 11:06 AM
You never tell the object what its new position is, add the following line
transform.position=pos;
i added the code and it works but the "player" does not move upwards, it is more like a teleportation + 1 in y-axis
Two ways to make the object move gradually are
1) $$anonymous$$ake the object move every frame using the UpdateFunction
void Update (){
transform.position+=Time.deltaTime*Vector3.up;
}
2) Add a rigidbody2D to the gameObject and give that object a momentum using AddForce
void Start(){
rigidbody2D.AddForce(new Vector2.up, Force$$anonymous$$ode2D.Impulse);
}
the problem is that the dead = true is in the OnCollisionEnter2D() function
void OnCollisionEnter2D(Collision2D collision) {
if (god$$anonymous$$ode)
return;
animator.SetTrigger("Death");
dead = true;
}
}
and in my Update() function the if(dead == true) does not work(does nothing)
void Update() {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (0)) {
didFlap = true;
//make dead bird jump and fall through ground like $$anonymous$$ARIO
if (dead == true) {
/* var pos = transform.position;
pos.y += upwardSpeed;
transform.position=pos;
Rigidbody2D.AddForce(new Vector2(0f, 1f),Force$$anonymous$$ode2D.Impulse); */
transform.position+=Time.deltaTime*Vector3.up;
}
}
ADDED::
I copied this code into the if statement that returns the "dead"
if (dead) {
transform.position+= upwardSpeed * Time.deltaTime*Vector3.up;
return;
}
Now it is bouncing.
Answer by Zhorky · Mar 02, 2015 at 01:43 PM
I would try to do this with
Rigidbody2D.AddForce(new Vector2(0f, 1f),ForceMode2D.Impulse);
tried to add but error appears:
An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.Force$$anonymous$$ode2D)'
What do i have to do?
rb2d = GetComponent(); rb2d.AddForce(new Vector2(0f, 1f),Force$$anonymous$$ode2D.Impulse);
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Can't jump in C# 1 Answer
Help with elevator Script 2 Answers
Move relative to objects rotation (C#) 1 Answer
c# script not doing anything. 2 Answers