Hey Guys,
I’m making a Doodle Jump clone at the moment and I’ve hit a snag.
I had it so that the character moved up the screen and the platforms moved above the player randomly, but I’ve been told that you’d get better performance on mobile if you had the player only really move in small area. So I have made the player and camera static, and am moving the bounce platforms down the screen.
Because my bounce platforms are moving and my player is slightly moving they both have Rigidbody2D’s attached along with BoxCollider2D’s as triggers and on the trigger I apply a jump force to the player to give the illusion of hitting a platform (whilst gravity is acting on him). My issues comes when trying to do that with the platforms…because the platforms are Kinematic as I want them to not gain momentum as they move down the screen (it looked hillarious to see them get faster and faster…but not what I wanted). My question is, how would I keep the platforms moving down at a rate then apply an Addforce type of momentum to a kinematic rigidbody?
My bounce platform currently…
using UnityEngine;
using System.Collections;
public class BouncePlatform : MonoBehaviour
{
public int jumpAmount;
public int moveSpeed;
private Transform myTransform;
private BoxCollider2D platformBoxCollider;
private Player player;
private Rigidbody2D rb;
void Start()
{
platformBoxCollider = GetComponent<BoxCollider2D>();
myTransform = this.transform;
rb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<Player>();
}
void Update()
{
rb.velocity = Vector2.down;
if(player == null)
{
return;
}
else
{
if(player.transform.position.y > myTransform.transform.position.y)
{
platformBoxCollider.enabled = true;
}
else
{
platformBoxCollider.enabled = false;
}
}
}
public void Jump()
{
rb.velocity = Vector2.down * jumpAmount;
}
}
The Jump() method doesn’t currently do anything and is called from the Player script…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Player : MonoBehaviour
{
public float moveFactor;
public int health;
public GameObject playerHealthText;
public bool isInvulnerable;
private Animator animationController;
private Rigidbody2D rb;
private float invulnerableTimer;
private BouncePlatform bouncePlatform;
public void Start()
{
invulnerableTimer = 5.0f;
animationController = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D>();
bouncePlatform = FindObjectOfType<BouncePlatform>();
}
void Update()
{
playerHealthText.GetComponent<Text> ().text = "Health: " + health.ToString();
health = Mathf.Clamp (health, 0, 3);
if (health <= 0)
{
//TODO Some sort of level end thing, this also probably belongs in a GameManager object?
Destroy(gameObject);
}
//invulnerability timer
if(isInvulnerable)
{
invulnerableTimer -= Time.deltaTime;
if(invulnerableTimer <= 0.0)
{
isInvulnerable = false;
}
}
//Camera things
OnBecameInvisible();
}
void FixedUpdate()
{
//Horizontal Movement
float playerXInput = Input.GetAxis("Horizontal");
float movePlayer = playerXInput * moveFactor;
rb.AddForce(new Vector2(movePlayer, 0f));
}
void OnBecameInvisible()
{
var cam = Camera.main;
var viewportPosition = cam.WorldToViewportPoint(transform.position);
var newPosition = transform.position;
if (viewportPosition.x > 1 || viewportPosition.x < 0)
{
newPosition.x = -newPosition.x;
}
transform.position = newPosition;
}
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.tag == "BouncePlatform")
{
Debug.Log("Hit: " + collider.gameObject.tag);
bouncePlatform.Jump();
rb.AddForce(Vector2.up * 500);
}
}
public void Damage(int damage)
{
if(!isInvulnerable)
{
animationController.SetTrigger ("damageTrigger");
health -= damage;
}
else
{
//TODO some sort of animation shit
return;
}
}
public void Heal(int heal)
{
health += heal;
}
public void IsInvulnerable(bool active)
{
isInvulnerable = active;
}
}
Any help would be appreciated as I’m at a bit of a loose end. Also I hope I’ve given you guys enough context/information, I essentially want a static camera/player with the platforms doing the ‘jumping’ whilst also constantly moving down the screen…
Thanks in advance!
Tim