MysteryGM

Unity_Beyblade_Part2

Jun 16th, 2021 (edited)
124
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 moveWarriorKnight : MonoBehaviour
  6.  
  7. {
  8.     public int speed; //We can change the speed value inside Unity because it is public
  9.     public Rigidbody rb;
  10.     public Vector3 velocity;
  11.     public float m_Thrust = 20f;
  12.  
  13.     Vector3 Vec;
  14.  
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.         rb.AddForce(velocity);
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.  
  25.         transform.Rotate(0, speed, 0); //Spins object on y axis
  26.         Vec = transform.localPosition;
  27.  
  28.         //Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;
  29.         Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;
  30.         Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;
  31.         transform.localPosition = Vec;
  32.     }
  33.  
  34.     //For when the top collides with another
  35.     public void OnCollisionEnter(Collision collisionInfo)
  36.     {
  37.  
  38.         if (collisionInfo.collider.tag == "Layer")
  39.         {
  40.             //Grab the other object
  41.             GameObject Other = collisionInfo.collider.gameObject;
  42.             //Get the velocity from the other object
  43.             Vector3 OtherVelocity = Other.GetComponent<Rigidbody>().velocity;
  44.             //We also need to grab this objects velocity before it changes
  45.             Vector3 MyVelocity = rb.velocity;
  46.  
  47.             //compare speeds
  48.             if (rb.velocity.magnitude < OtherVelocity.magnitude)
  49.             {
  50.                 //Debug.Log(this.name); //Debug code to check who lost
  51.                 //First disable this loosing object velocity, this way the add force has more impact
  52.                 rb.velocity = Vector3.zero;
  53.  
  54.                 //Next to prevent further collisions teleport it away
  55.                 rb.MovePosition(OtherVelocity.normalized * 0.05f); //Using some particles here to hide the jump will be smart
  56.  
  57.                 // if speed < other add force away from winner position
  58.                 rb.AddForce(OtherVelocity.normalized * m_Thrust);
  59.             }
  60.             else//This code now decides what happens to the winner
  61.             {
  62.                 rb.velocity = Vector3.zero;
  63.                 //We want the winner to move back a little
  64.                 rb.AddForce(-MyVelocity.normalized * m_Thrust/10f);
  65.             }
  66.  
  67.         }
  68.     }
  69.  
  70. }
  71.  
RAW Paste Data