Ledger Nano X - The secure hardware wallet
SHARE
TWEET

basic movement 2D WIP

a guest Apr 4th, 2020 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MAGD272Demo;
  5.  
  6. namespace MAGD272Demo
  7. {
  8.  
  9.     public class basicMovementPlatform : MonoBehaviour
  10.     {
  11.         [SerializeField] private bool facingRight;
  12.  
  13.         float horizontalValue;
  14.         float verticalValue;
  15.  
  16.         public float speed;
  17.  
  18.         Rigidbody2D rb;
  19.  
  20.         // reference var for our Animator Component
  21.         Animator animator;
  22.  
  23.         [SerializeField] private Transform spawnPoint;
  24.         [SerializeField] private GameObject bulletPrefab;
  25.  
  26.         private void Start()
  27.         {
  28.             // gets reference to Rigidbody2D on same GameObject
  29.             rb = GetComponent<Rigidbody2D>();
  30.  
  31.             // get reference to Animator on the same GameObject
  32.             animator = GetComponent<Animator>();
  33.         }
  34.  
  35.         // Update is called once per frame
  36.         void Update()
  37.         {
  38.             CheckAxes();
  39.             Animate();
  40.             //PositionMove();
  41.             FlipSprite();
  42.         }
  43.  
  44.         void FixedUpdate()
  45.         {
  46.             // call physics-related methods like setvelocity in FixedUpdate
  47.  
  48.             // Use either Set Velocity or ForceMove. Not both. They are different movement ideas
  49.  
  50.             SetVelocity();
  51.  
  52.             //ForceMove();
  53.         }
  54.  
  55.         void Animate()
  56.         {
  57.             // use the absolute value of horizontal so that "horizontalValue" parameter is
  58.             // only between 0 and +number
  59.             animator.SetFloat("horizontalValue", Mathf.Abs(horizontalValue));
  60.         }
  61.  
  62.         void CheckAxes()
  63.         {
  64.             horizontalValue = Input.GetAxis("Horizontal") * speed;
  65.             //verticalValue = Input.GetAxis("Vertical") * speed;
  66.             verticalValue = 0f;
  67.             //print(horizontalValue + " - " + verticalValue);
  68.         }
  69.  
  70.         void FlipSprite()
  71.         {
  72.             if (horizontalValue < 0 && facingRight == true)
  73.             {
  74.                 transform.Rotate(0, 180, 0);
  75.                 facingRight = false;
  76.             }
  77.             else if (horizontalValue > 0 && facingRight == false)
  78.             {
  79.                 transform.Rotate(0, 180, 0);
  80.                 facingRight = true;
  81.             }
  82.         }
  83.  
  84.  
  85.         // the three functions below control our movement, we should only be calling one of these at anytime.
  86.  
  87.         void PositionMove()
  88.         {
  89.             // changing by addressing its Transform.position
  90.             //transform.position += new Vector3(horizontalValue, verticalValue, 0);
  91.             transform.Translate(new Vector3(horizontalValue, verticalValue, 0));
  92.         }
  93.  
  94.         void SetVelocity()
  95.         {
  96.             // assigns value to our rigidbody's velocity
  97.             rb.velocity = new Vector2(horizontalValue, verticalValue + rb.velocity.y);
  98.         }
  99.  
  100.         void ForceMove()
  101.         {
  102.             rb.AddForce(new Vector2(horizontalValue, verticalValue));
  103.         }
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top