ページ 11

camera追従について

Posted: 2013年10月14日(月) 10:48
by icon00
playerのうしろを追従するプログラムを組んでいるのですが、Y軸だけでなくXとZ軸のplayer回転にも対応させたい。

で、組んだのがこれ。

コード:


var target : Transform;


// The distance in the x-z plane to the target
var distance = 30.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu

function LateUpdate () {
	// Early out if we don't have a target

	if (!target)
		return;
	
	// Calculate the current rotation angles
	var wantedRotationAngle_x = target.localEulerAngles.x;
	
	//var wantedHeight_x = target.position.x + height;
	
	var wantedRotationAngle_y = target.localEulerAngles.y;
	
	//var wantedHeight_y = target.position.y + height;
	
	var wantedRotationAngle_z = target.localEulerAngles.z;
	
	//var wantedHeight_z = target.position.z + height;
	
	var currentRotationAngle_x = transform.localEulerAngles.x;
	
	//var currentHeight_x = transform.position.x;
	
	var currentRotationAngle_y = transform.localEulerAngles.y;
	
	//var currentHeight_y = transform.position.y;
	
	var currentRotationAngle_z = transform.localEulerAngles.z;
	
	//var currentHeight_z = transform.position.z;
	
	// Damp the rotation around the y-axis
	currentRotationAngle_x = Mathf.LerpAngle (currentRotationAngle_x, wantedRotationAngle_x, rotationDamping * Time.deltaTime);
	currentRotationAngle_y = Mathf.LerpAngle (currentRotationAngle_y, wantedRotationAngle_y, rotationDamping * Time.deltaTime);
	currentRotationAngle_z = Mathf.LerpAngle (currentRotationAngle_z, wantedRotationAngle_z, rotationDamping * Time.deltaTime);
 
	// Damp the height
	
	//currentHeight_x = Mathf.Lerp(currentHeight_x, wantedHeight_x, heightDamping * Time.deltaTime);
	//currentHeight_y = Mathf.Lerp(currentHeight_y, wantedHeight_y, heightDamping * Time.deltaTime);
	//currentHeight_z = Mathf.Lerp(currentHeight_z, wantedHeight_z, heightDamping * Time.deltaTime);
	
	// Convert the angle into a rotation
	var currentRotation = Quaternion.Euler(currentRotationAngle_x, currentRotationAngle_y,currentRotationAngle_z);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	//if(target.rotation.y)
	
	//transform.Rotate(target.localEulerAngles.x,target.localEulerAngles.y,target.localEulerAngles.z);

	transform.position = target.position;

	transform.position -= currentRotation * Vector3.forward * distance;
	
	// Set the height of the camera
	//transform.position.y = currentHeight_y;
	
	// Always look at the target
	transform.LookAt (target);
}

やっぱりうまくいかない。

どなたかわかる人いましたら、よろしくお願いいたします。

Re: camera追従について

Posted: 2013年10月14日(月) 13:43
by h2so5
APIからUnityだと思いますが、ソースコードだけ載せるのではなく開発環境を明記してください。

リファレンスを読んだ限りだと、カメラ自体の角度を変化させるにはlookAtの第二引数で上方向のベクトルを指定する必要がありそうです。
デフォルトでは (0, 1, 0)で常にy軸方向が上になってしまうので。

Re: camera追従について

Posted: 2013年10月14日(月) 15:20
by icon00
失礼しました。

開発環境はUnity&Javascript

今現在は、Y軸の回転は出来ている。が、XとZ軸に回転をかけると挙動がおかしい。

最悪、playerとcameraを親子関係にするのも考えたが、leapさせたいので却下。

補足


h2so5 さんにならって

コード:


transform.LookAt (target,Vector3(target.position.x,target.position.y,target.position.z));

にしたらcameraはXとZ軸にも回転してくれたが、わずかにずれる(斜めになる)。

なぜだ。

ちなみにlookAtの第二引数を

target.position

から

target.rotation

にしたらcameraがぐるぐるまわる。

Re: camera追従について

Posted: 2013年10月14日(月) 18:41
by Idra
Unity使ったこと無いけど、
カメラの上方向にcurrentRotation * Vector3.upを設定すればいいんじゃないの?

Re: camera追従について

Posted: 2013年10月16日(水) 12:30
by icon00
返信遅れました。

Idraさんにならってやってみた所、最終的には正しく回転してくれるが、過程がおかしい

余計な回転が加わる。絶対酔っぱらうぞこれ。

x,y,zの回転がいちいち計算されるせい?