MyUnityTest

 2023.10.12 unityC#  
 2023.09.18 unityC#  
 2023.09.18 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.15 unityC#  
 2023.09.14 unityC#  
 2023.09.13 unityC#  
 2023.09.13 unityC#  

CameraWithPlayer


//cameraにアタッチ
using UnityEngine;
public class CameraMove : MonoBehaviour
{
 [SerializeField] Transform playerTr; // プレイヤーのTransform
 [SerializeField] Vector3 cameraOrgPos = new Vector3(0, 0, -10f); // カメラの初期位置位置 
Vector2 camaraMaxPos = new Vector2(22, 6); // カメラの右上限界座標
Vector2 camaraMinPos = new Vector2(-22,-6); // カメラの左下限界座標
    void LateUpdate()
 {
  Vector3 playerPos = playerTr.position; // プレイヤーの位置
   Vector3 camPos = transform.position; // カメラの位置
   // 滑らかにプレイヤーの場所に追従
    camPos = Vector3.Lerp(transform.position, playerPos + cameraOrgPos, 3.0f * Time.deltaTime);
   // カメラの位置を制限
   camPos.x = Mathf.Clamp(camPos.x, camaraMinPos.x, camaraMaxPos.x);
   camPos.y = Mathf.Clamp(camPos.y, camaraMinPos.y, camaraMaxPos.y);
   camPos.z = -10f;
   transform.position = camPos;
    }
}
///////////////////////////
using UnityEngine;

public class CameraScript : MonoBehaviour
{
    public GameObject player;
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
      Vector3 playerPos = player.transform.position;
      //カメラとプレイヤーの位置を同じにする
      transform.position = new Vector3(playerPos.x, playerPos.y, -10);
    }
}

月別
固定ページ