SHARE
TWEET

Fireball

Ryoh Feb 14th, 2020 (edited) 82 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.  
  5. public class Fireball : MonoBehaviour
  6. {
  7.     [SerializeField] float jumpHeight = 4;
  8.     [SerializeField] float timeToJumpApex = .4f;
  9.     [SerializeField] float moveSpeed = 6;
  10.  
  11.     float gravity;
  12.     float bounceVelocity;
  13.     Vector3 velocity;
  14.  
  15.     private bool turnAround;
  16.  
  17.     ProjectileController2D controller;
  18.  
  19.     void Start()
  20.     {
  21.         controller = GetComponent<ProjectileController2D>();
  22.  
  23.         gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
  24.         bounceVelocity = Mathf.Abs(gravity) * timeToJumpApex;
  25.     }
  26.  
  27.     void Update()
  28.     {
  29.         if (controller.collisions.above)
  30.         {
  31.             velocity.y = 0;
  32.         }
  33.  
  34.         if (controller.collisions.below)
  35.         {
  36.             velocity.y = bounceVelocity;
  37.         }
  38.  
  39.         if(controller.collisions.left || controller.collisions.right)
  40.         {
  41.             turnAround = !turnAround;
  42.         }
  43.        
  44.         // This is a terniary operation. It is saying if turnAround is true then we use -1, otherwise we use 1.
  45.         // Those 1's are how we decide our direction
  46.         velocity.x = moveSpeed * ((turnAround) ? -1 : 1);
  47.         velocity.y += gravity * Time.deltaTime;
  48.         controller.Move(velocity * Time.deltaTime);
  49.     }
  50. }
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