Make the world bounce instead of player

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

I don’t see how moving more things (the platforms) gives better performance than moving a single thing (the player) with a camera attached. Sounds like bad advice to me.

Anyhow, Kinematic Rigidbody2D are not affected by collisions, gravity or forces you apply. Behind the scenes in Box2D, they have zero mass and zero inertia so they are effectively infinitely massive, the same as static bodies.

The difference is however that you can either re-position them directly via the Transform.position, use Rigidbody2D.MovePosition or directly set their velocity with Rigidbody2D.velocity to move them.

Ah ok, so it would be just as efficient to have the player moving infinitely through the game space? If that is the case then I would need to reposition the platforms when they went outside of the camera view…as currently they are picking up a space in the world and then repositioning…

using UnityEngine;
using System.Collections;

public class PlatformMover : MonoBehaviour 
{
	public float lowerLimit;
	public float distanceToMoveUp;
	
	private Rigidbody2D rb;
	
	void Start()
	{
		rb = GetComponent<Rigidbody2D>();
	}

	void Update()
	{
		if(transform.position.y < lowerLimit)
		{
			rb.position = new Vector3 (transform.position.x, transform.position.y + distanceToMoveUp, transform.position.z);
			rb.velocity = new Vector2 (0, 0);
		}
	}
}

This script repositions the platform when it hits a point, I have tried to use OnBecameInvisible() to do the moving but, it doesn’t seem to want to work.

Could you give me a nudge in the direction of reading the bottom of the Screens viewport?

Thanks! Tim