Hey !
I have a small problem with my game, i want to create a game similar of this game (the movement) :
Space is Key, a free online Puzzle & Skill game brought to you by Armor Games. Version 1.04 - Fixed jumping straight after spawning.
Space is Key. Simple concept, but can you do it?
Time your jumps carefully, or you turn into tiny particles!...
And i made this : (I’m new in Unity :/)
void jump()
{
if (Input.GetKey(KeyCode.Space) && Global.isGrounded == true)
{
rigidbody2D.AddForce(Vector3.up * jumpForce);
isJumped = true;
}
else if(Global.isGrounded == false)
{
rigidbody2D.AddForce(-Vector3.up * jumpForce / slowJump);
}
if (Global.isGrounded == false)
{
targetAngles = transform.eulerAngles - 180f * Vector3.forward;
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, speedRotation * Time.deltaTime);
}
}
//MOVEMENT
void Movement()
{
transform.Translate(Vector2.right / slowSpeed, Space.World);
}
I dont know how can i improve my movement and my jump… Can you help me ?
Rather than using AddForce(), I’d do the jump mathematically. Here is some example code to get you part of the way there:
using UnityEngine;
using System.Collections;
public class MoveAndJumpAndFlip : MonoBehaviour {
public float speed = 4.0f;
public float jumpTime = 1.2f;
public float jumpHeight = 1.4f;
private float xPos;
private float yPos;
private float deltaY = 0.0f;
private bool isJumping = false;
void Start () {
xPos = transform.position.x;
yPos = transform.position.y;
}
void Update () {
if (!isJumping && Input.GetKeyDown (KeyCode.Space)) {
StartCoroutine(Jump());
}
Vector3 newPos = transform.position;
xPos += speed * Time.deltaTime;
newPos.x = xPos;
newPos.y = yPos + deltaY;
rigidbody2D.MovePosition (newPos);
}
IEnumerator Jump() {
if (isJumping) yield break;
isJumping = true;
float timer = 0.0f;
while (timer <= 1.0f) {
transform.rotation = Quaternion.AngleAxis (-180.0f * timer, Vector3.forward);
deltaY = Mathf.Sin (timer * Mathf.PI) * jumpHeight;
timer += Time.deltaTime / jumpTime;
yield return null;
}
deltaY = 0.0f;
transform.rotation = Quaternion.identity;
isJumping = false;
}
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "Obstical") {
Debug.Log ("Hit an obstical");
}
if (col.tag == "Star") {
Debug.Log ("Hit star");
}
}
}
I don’t know to what extent you are duplicating the game at the link, but ‘yPos’ will be the height of the player object. So as you move down, you will set yPos to a new value. This code assumes that the obstacle blocks have box colliders set to trigger…as do the stars. The “surface” the player block runs along does not have collider since it is simulated.