0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】ratationでpositionやAddForoceの値を変化させる

Posted at
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// キーを押したら、ゲームオブジェクトを作って投げる
public class OnKeyPress_Throw : MonoBehaviour
{
	public GameObject newPrefab; // プレハブ:Inspectorで指定
	public string pushKey = "z"; // 押すキー:Inspectorで指定
	public float throwX = 0;     // 投げる力:Inspectorで指定
	public float throwY = 3;     // 投げる力:Inspectorで指定
	public float throwZ = 4;     // 投げる力:Inspectorで指定
	public float offsetX = 0f;   // 作る位置の高さオフセット:Inspectorで指定
	public float offsetY = 1f;   // 作る位置の高さオフセット:Inspectorで指定
	public float offsetZ = 0.5f; // 作る位置の高さオフセット:Inspectorで指定

	bool pushFlag = false;

	void Update() // ずっと、行う
	{
		if (Input.GetKey(pushKey)) // もし、キーが押されて
		{
			if (pushFlag == false) // 押しっぱなしでなければ
			{
				pushFlag = true; // 押した状態に
				Vector3 newPos = this.transform.position;
				Vector3 offset = new Vector3(offsetX, offsetY, offsetZ);
				offset = this.transform.rotation * offset;
				newPos = newPos + offset;

				// プレハブからゲームオブジェクトを作って
				GameObject newGameObject = Instantiate(newPrefab) as GameObject;
				newGameObject.transform.position = newPos;
				// 投げる
				Rigidbody rbody = newGameObject.GetComponent<Rigidbody>();
				Vector3 throwV = new Vector3(throwX, throwY, throwZ);
				throwV = this.transform.rotation * throwV;
				rbody.AddForce(throwV, ForceMode.Impulse);
			}
		} else
		{
			pushFlag = false; 		// 押した状態解除
		}
	}
}


このコードで当オブジェクトにoffset足した座標を得られる
offset = this.transform.rotation * offset;
newPos = newPos + offset;

このコードで対象オブジェクトに力を与えられる
throwV = this.transform.rotation * throwV;
rbody.AddForce(throwV, ForceMode.Impulse);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?