camera追従について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
icon00

camera追従について

#1

投稿記事 by icon00 » 12年前

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);
}

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

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

アバター
h2so5
副管理人
記事: 2212
登録日時: 15年前
住所: 東京
連絡を取る:

Re: camera追従について

#2

投稿記事 by h2so5 » 12年前

APIからUnityだと思いますが、ソースコードだけ載せるのではなく開発環境を明記してください。

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

icon00

Re: camera追従について

#3

投稿記事 by icon00 » 12年前

失礼しました。

開発環境は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がぐるぐるまわる。

Idra

Re: camera追従について

#4

投稿記事 by Idra » 12年前

Unity使ったこと無いけど、
カメラの上方向にcurrentRotation * Vector3.upを設定すればいいんじゃないの?

icon00

Re: camera追従について

#5

投稿記事 by icon00 » 12年前

返信遅れました。

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

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

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

閉鎖

“C言語何でも質問掲示板” へ戻る