Advertisement
XxSonic4xX

C# Code for Unity (See unity answers for full post)

Dec 22nd, 2017
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //My code (unity, C#)
  2.  
  3. using UnityEngine;
  4.  
  5. public class SpriteMovement : MonoBehaviour {
  6.     //Defining speed for horizontal movement, going to be used later in script.
  7.     public int speedHori = 10;
  8.     public int speedVert = 10;
  9.     public int jumptime = 0;
  10.     private Vector2 movement;
  11.     private Vector2 movementUp;
  12.     //Defining the Rigidbody2D (sprite) to move in Unity.
  13.     public Rigidbody2D rb;
  14.     void Start () {
  15.         rb = GetComponent<Rigidbody2D>();
  16.         movement = new Vector2(speedHori, 0);
  17.         movementUp = new Vector2(0, speedVert);
  18.     }
  19.     //Using FixedUpdate to mess with 2d phys.
  20.     void FixedUpdate () {
  21.         if (Input.GetKey("d"))
  22.         {
  23.             rb.AddForce(movement, ForceMode2D.Force);
  24.         }
  25.         if (Input.GetKey("a"))
  26.         {
  27.             rb.AddForce(-movement, ForceMode2D.Force);
  28.         }
  29.         if (jumptime < 3)
  30.         {
  31.             if (Input.GetKeyDown(KeyCode.Space))
  32.             {
  33.                 rb.AddForce(movementUp, ForceMode2D.Impulse);
  34.                 jumptime = jumptime + 1;
  35.                
  36.             }
  37.         }
  38.         if (jumptime == 3)
  39.         {
  40.             jumptime -= 3;
  41.         }
  42.     }
  43. }
  44.  
  45. //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