投げる v2|Unity5 3D

投げる v2 |Unity5 3D

投げる v2|Unity5 3D : ZIPファイル(554kb)

Unity5 3D で、球を投げるシミュレーションを作りました。四方の壁やお互いのボールにぶつかると、跳ね返ります。

C#

Drag_3D_2.cs

using UnityEngine;
using System.Collections;

public class Drag_3D_2 : MonoBehaviour {

	float offsetX;
	float offsetY;
	float oldX = 0;
	float oldY = 0;
	float vx = 0;
	float vy = 0;
	GameObject camera1;

	void Start(){
		camera1 = GameObject.Find ("Main Camera");
	}

	// Update is called once per frame
	void FixedUpdate () {

		GameObject obj = null;
		if(Input.GetMouseButtonDown(0))
		{

			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit = new RaycastHit();

			if (Physics.Raycast(ray, out hit))
			{
				obj = hit.collider.gameObject;
				if (obj.name == "ball"||obj.name == "ball2") 
				{
					obj.GetComponent<Rigidbody> ().isKinematic = true;
					Vector3 screen_point1 = Input.mousePosition;
					//マウス位置のZ座標にカメラのZ座標位置の差分を設定
					screen_point1.z = -1 * camera1.transform.position.z;
					//上記マウス位置を元に、ワールド空間の位置を取得
					Vector3 mousePos = Camera.main.ScreenToWorldPoint (screen_point1);

					Vector3 spritePos = obj.transform.position;
					float dx = mousePos.x - spritePos.x;
					float dy = mousePos.y - spritePos.y;
					float dz = mousePos.z - spritePos.z;
					float rad = Mathf.Atan2 (dy, dx);
					float dist = Mathf.Sqrt (dx * dx + dy * dy + dz * dz);
					offsetX = Mathf.Cos (rad) * dist;
					offsetY = Mathf.Sin (rad) * dist;

					oldX = mousePos.x;
					oldY = mousePos.y;
				}
			}
		}
		else {
			if (obj) {
				obj.GetComponent<Rigidbody> ().isKinematic = false;
			}
		}

		if (Input.GetMouseButton (0)) 
		{
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			RaycastHit hit = new RaycastHit ();

			if (Physics.Raycast (ray, out hit)) 
			{
				obj = hit.collider.gameObject;
				if (obj.name == "ball"||obj.name == "ball2") 
				{
                                        obj.GetComponent<Rigidbody> ().isKinematic = true;
					Vector3 screen_point1 = Input.mousePosition;
					//マウス位置のZ座標にカメラのZ座標位置の差分を設定
					screen_point1.z = -1 * camera1.transform.position.z;
					//上記マウス位置を元に、ワールド空間の位置を取得
					Vector3 mouseMovePos = Camera.main.ScreenToWorldPoint (screen_point1);

					Vector3 sprite = obj.transform.position;
					sprite.x = mouseMovePos.x - offsetX;
					sprite.y = mouseMovePos.y - offsetY;
					obj.transform.position = sprite;
				}
			}
		}
		else{
			if (obj) {
				obj.GetComponent<Rigidbody> ().isKinematic = false;
			}
		}

		if (Input.GetMouseButtonUp (0)) {
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			RaycastHit hit = new RaycastHit ();
			if (Physics.Raycast (ray, out hit)) {
				obj = hit.collider.gameObject;
				if (obj.name == "ball" || obj.name == "ball2") {
					
					Vector3 sprite = obj.transform.position;
					vx = sprite.x - oldX;
					vy = sprite.y - oldY;

					obj.GetComponent<Rigidbody> ().AddForce (new Vector3 (vx, vy, 0));
				}
			} 
		
		} else {
			if (obj) {
				obj.GetComponent<Rigidbody> ().isKinematic = false;
			}
		}
	}
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です