Created
June 17, 2017 23:27
-
-
Save Drenerdo/2960d307940a4c46d32d6c5d1ef099ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class WeaponThrow : MonoBehaviour { | |
public Rigidbody rigidbody; | |
void Update () { | |
if (Input.GetKeyDown (KeyCode.Space)) { | |
StartCoroutine(Throw(18.0f, 1.0f, Camera.main.transform.forward, 2.0f)); | |
} | |
} | |
IEnumerator Throw(float dist, float width, Vector3 direction, float time) { | |
Vector3 pos = transform.position; | |
float height = transform.position.y; | |
Quaternion q = Quaternion.FromToRotation (Vector3.forward, direction); | |
float timer = 0.0f; | |
rigidbody.AddTorque (0.0f, 400.0f, 0.0f); | |
while (timer < time) { | |
float t = Mathf.PI * 2.0f * timer / time - Mathf.PI/2.0f; | |
float x = width * Mathf.Cos(t); | |
float z = dist * Mathf.Sin (t); | |
Vector3 v = new Vector3(x,height,z+dist); | |
rigidbody.MovePosition(pos + (q * v)); | |
timer += Time.deltaTime; | |
yield return null; | |
} | |
rigidbody.angularVelocity = Vector3.zero; | |
rigidbody.velocity = Vector3.zero; | |
rigidbody.rotation = Quaternion.identity; | |
rigidbody.MovePosition (pos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will be tweaking this functionality