Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //My code (unity, C#)
- using UnityEngine;
- public class SpriteMovement : MonoBehaviour {
- //Defining speed for horizontal movement, going to be used later in script.
- public int speedHori = 10;
- public int speedVert = 10;
- public int jumptime = 0;
- private Vector2 movement;
- private Vector2 movementUp;
- //Defining the Rigidbody2D (sprite) to move in Unity.
- public Rigidbody2D rb;
- void Start () {
- rb = GetComponent<Rigidbody2D>();
- movement = new Vector2(speedHori, 0);
- movementUp = new Vector2(0, speedVert);
- }
- //Using FixedUpdate to mess with 2d phys.
- void FixedUpdate () {
- if (Input.GetKey("d"))
- {
- rb.AddForce(movement, ForceMode2D.Force);
- }
- if (Input.GetKey("a"))
- {
- rb.AddForce(-movement, ForceMode2D.Force);
- }
- if (jumptime < 3)
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- rb.AddForce(movementUp, ForceMode2D.Impulse);
- jumptime = jumptime + 1;
- }
- }
- if (jumptime == 3)
- {
- jumptime -= 3;
- }
- }
- }
- //The var Jumptime is used to track the amount of jumps the player has made. It should have a 3 second wait before resetting the value.
Advertisement
RAW Paste Data
Copied
Advertisement