Advertisement
Guest User

Untitled

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