客観視点のカメラを作成
![E81ad206 4436 4e55 971b 4e430cdcec35](https://codegenius.org/uploads/slide/image/2066/e81ad206-4436-4e55-971b-4e430cdcec35.jpeg)
第三者視点のカメラ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
public float smooth = 3.0f;
private Transform standardPos;
private Transform frontPos;
// ★ポイント
private bool bQuickSwitch = false;
void Start () {
// (テクニック)1台のカメラで複数の視点を実現する。
standardPos = GameObject.Find("CamPos").transform;
frontPos = GameObject.Find ("FrontPos").transform;
// カメラの初期位置は「standardPos」の位置
transform.position = standardPos.position;
// 初期の向きは「standardPos」の向き
transform.forward = standardPos.forward;
}
// カメラの切り替えはこの中で行う。
void FixedUpdate () {
if (Input.GetKey (KeyCode.F)) {
SetCameraFrontPositionView ();
} else {
SetCameraNomalPositionView ();
}
}
void SetCameraNomalPositionView(){
if (bQuickSwitch == false) {
transform.position = Vector3.Lerp (transform.position, standardPos.position, Time.fixedDeltaTime * smooth);
transform.forward = Vector3.Lerp (transform.forward, standardPos.forward, Time.fixedDeltaTime * smooth);
} else { // フロントポジションからスタンダードポジションには素早く移動する。
transform.position = standardPos.position;
transform.forward = standardPos.forward;
bQuickSwitch = false;
}
}
void SetCameraFrontPositionView(){
bQuickSwitch = true;
transform.position = frontPos.position;
transform.forward = frontPos.forward;
}
}
![14fc4d3e 22cc 4fc8 a780 7c9fe47f546e](https://codegenius.org/uploads/slide/image/2067/14fc4d3e-22cc-4fc8-a780-7c9fe47f546e.jpeg)
![346c8d48 66f7 43e2 b158 c9f4a001c8a9](https://codegenius.org/uploads/slide/image/2068/346c8d48-66f7-43e2-b158-c9f4a001c8a9.jpeg)
![85c27218 cd37 448c 9d3f e962c782205c](https://codegenius.org/uploads/slide/image/2069/85c27218-cd37-448c-9d3f-e962c782205c.jpeg)
![E81ad206 4436 4e55 971b 4e430cdcec35](https://codegenius.org/uploads/slide/image/2066/e81ad206-4436-4e55-971b-4e430cdcec35.jpeg)
第三者視点のカメラ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
public float smooth = 3.0f;
private Transform standardPos;
private Transform frontPos;
// ★ポイント
private bool bQuickSwitch = false;
void Start () {
// (テクニック)1台のカメラで複数の視点を実現する。
standardPos = GameObject.Find("CamPos").transform;
frontPos = GameObject.Find ("FrontPos").transform;
// カメラの初期位置は「standardPos」の位置
transform.position = standardPos.position;
// 初期の向きは「standardPos」の向き
transform.forward = standardPos.forward;
}
// カメラの切り替えはこの中で行う。
void FixedUpdate () {
if (Input.GetKey (KeyCode.F)) {
SetCameraFrontPositionView ();
} else {
SetCameraNomalPositionView ();
}
}
void SetCameraNomalPositionView(){
if (bQuickSwitch == false) {
transform.position = Vector3.Lerp (transform.position, standardPos.position, Time.fixedDeltaTime * smooth);
transform.forward = Vector3.Lerp (transform.forward, standardPos.forward, Time.fixedDeltaTime * smooth);
} else { // フロントポジションからスタンダードポジションには素早く移動する。
transform.position = standardPos.position;
transform.forward = standardPos.forward;
bQuickSwitch = false;
}
}
void SetCameraFrontPositionView(){
bQuickSwitch = true;
transform.position = frontPos.position;
transform.forward = frontPos.forward;
}
}
![14fc4d3e 22cc 4fc8 a780 7c9fe47f546e](https://codegenius.org/uploads/slide/image/2067/14fc4d3e-22cc-4fc8-a780-7c9fe47f546e.jpeg)
![346c8d48 66f7 43e2 b158 c9f4a001c8a9](https://codegenius.org/uploads/slide/image/2068/346c8d48-66f7-43e2-b158-c9f4a001c8a9.jpeg)
![85c27218 cd37 448c 9d3f e962c782205c](https://codegenius.org/uploads/slide/image/2069/85c27218-cd37-448c-9d3f-e962c782205c.jpeg)
客観視点のカメラを作成