Advertisement
Guest User

ShipRigidbodyMovementController

a guest
Aug 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Experimental.VFX;
  5. using UnityEngine;
  6.  
  7. public class ShipRigidbodyMovementController : MonoBehaviour
  8. {
  9.     public float velocity = 15;
  10.     public float rotationSpeed = 180;
  11.     public ShipMovementType movementType;
  12.  
  13.     private ShipInputController inputController;
  14.     private new Rigidbody rigidbody;
  15.     public VisualEffect shipEngineThrustLeft;
  16.     public VisualEffect shipEngineThrustRight;
  17.  
  18.     private void Start()
  19.     {
  20.         inputController = GetComponent<ShipInputController>();
  21.         rigidbody = GetComponent<Rigidbody>();
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         switch (movementType)
  27.         {
  28.             case ShipMovementType.ManualRotation:
  29.                 UpdateMoveManualRotation();
  30.                 return;
  31.  
  32.             case ShipMovementType.AdaptiveRotation:
  33.                 UpdateMoveAdaptiveRotation();
  34.                 return;
  35.             case ShipMovementType.Slide:
  36.                 UpdateMoveSlide();
  37.                 return;
  38.             default:
  39.                 UpdateMoveAdaptiveRotation();
  40.                 return;
  41.         }
  42.     }
  43.     private void UpdateMoveSlide()
  44.     {
  45.         this.rigidbody.AddForce(inputController.horizontal * Vector3.right * velocity * Time.deltaTime);
  46.         this.rigidbody.AddForce(inputController.vertical * Vector3.forward * velocity * Time.deltaTime);
  47.     }
  48.  
  49.     private void UpdateMoveManualRotation()
  50.     {
  51.         this.rigidbody.AddForce(inputController.vertical * this.transform.forward * velocity * Time.deltaTime);
  52.         var rotationAmount = rotationSpeed * Time.deltaTime * inputController.horizontal;
  53.         this.rigidbody.AddTorque(0, rotationAmount, 0);
  54.     }
  55.  
  56.     private void UpdateMoveAdaptiveRotation()
  57.     {
  58.         var inputDirection = new Vector3(inputController.horizontal, 0, inputController.vertical);
  59.         var thrust = Vector3.Dot(inputDirection.normalized, this.transform.forward);
  60.         var rotation = Vector3.Dot(inputDirection.normalized, this.transform.right); //Is the controller aimed left or right?
  61.  
  62.         this.rigidbody.AddForce(thrust * inputDirection.magnitude * this.transform.forward * velocity * Time.deltaTime/* * inputDirection.magnitude*/);
  63.         var rotationAmount = rotationSpeed * Time.deltaTime * rotation;
  64.         this.rigidbody.AddTorque(0, rotationAmount, 0);
  65.  
  66.         shipEngineThrustLeft.SetFloat("fxVelocity", thrust);
  67.         shipEngineThrustRight.SetFloat("fxVelocity", thrust);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement