- Home /
(sprite2D) Movement like this game...
Hey !
I have a small problem with my game, i want to create a game similar of this game (the movement) : http://armorgames.com/play/10912/space-is-key
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 ?
Answer by robertbu · Sep 16, 2014 at 05:54 PM
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.
Ohhh thx dude !
It work perfectly ! <3
I have an other question now, i play Garry's $$anonymous$$od (S$$anonymous$$m game) and i can dev in this game. They have a function to find if my var is changed, and execute just one time, like :
if(changed(Var) && Var == true){ print("hello")}
I havent loop if my var == true.
How can i do that in Unity ?
I want to do that because i want to save my position when my cube hit the world for the fist time
There's nothing like the 'changed' function in Unity/C#. You'll have to code the logic yourself...save a copy of the original value and compare.
Yep, i made this :) thx Robertbu.
Well, for me its ok. Problem fixed.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Rotating a object towards a direction then move it 0 Answers
Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers
how do i make an object always face the player? 5 Answers
Joystick 2D Sprite Rotation and Movement 0 Answers