- Home /
Question by
Jammer3000 · Feb 26, 2014 at 05:09 AM ·
spritespritesbounce
How to add floating bounce effect to sprite?
I'm trying to add a floating bounce effect to one of my sprites but I don't know the best way to approach this and the way I'm trying(code below) isn't really working?
#pragma strict
public var gameObjectPos : Transform;
function Update () {
if (gameObjectPos.transform.position.y < 2) {
gameObject.transform.position += Vector3.up * 0.02;
}
if (gameObjectPos.transform.position.y == 2) {
gameObject.transform.position += Vector3.down * 0.02;
Debug.Log("To High");
}
}
Comment
Best Answer
Answer by robertbu · Feb 26, 2014 at 05:16 AM
Here is an alternate:
#pragma strict
var floatSpan = 2.0;
var speed = 1.0;
private var startY : float;
function Start() {
startY = transform.position.y;
}
function Update() {
transform.position.y = startY + Mathf.Sin(Time.time * speed) * floatSpan / 2.0;
}
It uses the start position as the midpoint of the bounce. 'floatSpan' is the height (bottom to top) of the bounce.
This works great thanks robertbu would you be able to answer something else for me, I've been stuck on this question for a while and figured I would ask
Your answer
Follow this Question
Related Questions
How to make a character bounce? 0 Answers
how do i make an object always face the player? 5 Answers
How to scroll a sprite? 0 Answers
Images won't import as sprites? 1 Answer