gpt4 book ai didi

c# - Unity相机起始位置

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:33 25 4
gpt4 key购买 nike

你好,我为我正在制作的 RTS 游戏创建了一个摄像头,但在开始时摄像头处于 minHight 位置,我无法弄清楚如何更改它希望有人帮助代码有点乱它只是为了这里的测试是我希望它从给定的 maxHight 位置开始的代码,而不是从 minHight 开始的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RtsCamera : MonoBehaviour {


public float zoomSensitivy = 15;
private Transform m_Transform;
public float screenEdgeBorder = 25f;
public bool useScreenEdgeInput = true;
public float screenEdgeMovementSpeed = 3f;

// zoom stuff
public float heightDampening = 5f;
public float scrollWheelZoomingSensitivity = 25f;
public float maxHeight = 10f; //maximal height
public float minHeight = 15f; //minimnal height
public LayerMask groundMask = -1;
public bool autoHeight = true;
public bool limitMap = true;
public float limitX = 50f; //x limit of map
public float limitZ = 50f; //z limit of map
private float zoomPos = 0; //value in range (0, 1) used as t in Matf.Lerp
public bool useScrollwheelZooming = true;

Vector3 tempPos;


public string zoomingAxis = "Mouse ScrollWheel";

private float ScrollWheel
{
get { return Input.GetAxis(zoomingAxis); }
}

private Vector2 MouseAxis
{
get { return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); }
}
private Vector2 MouseInput
{
get { return Input.mousePosition; }
}


private void Start()
{

m_Transform = transform;

}
// Update is called once per frame
void Update () {
Move();
LimitPosition();
HeightCalculation();







}
private void Move()
{


if (useScreenEdgeInput)
{
Vector3 desiredMove = new Vector3();

Rect leftRect = new Rect(0, 0, screenEdgeBorder, Screen.height);
Rect rightRect = new Rect(Screen.width - screenEdgeBorder, 0, screenEdgeBorder, Screen.height);
Rect upRect = new Rect(0, Screen.height - screenEdgeBorder, Screen.width, screenEdgeBorder);
Rect downRect = new Rect(0, 0, Screen.width, screenEdgeBorder);

desiredMove.x = leftRect.Contains(MouseInput) ? -1 : rightRect.Contains(MouseInput) ? 1 : 0;
desiredMove.z = upRect.Contains(MouseInput) ? 1 : downRect.Contains(MouseInput) ? -1 : 0;

desiredMove *= screenEdgeMovementSpeed;
desiredMove *= Time.deltaTime;
desiredMove = Quaternion.Euler(new Vector3(0f, transform.eulerAngles.y, 0f)) * desiredMove;
desiredMove = m_Transform.InverseTransformDirection(desiredMove);

m_Transform.Translate(desiredMove, Space.Self);
}

}
private void LimitPosition()
{
if (!limitMap)
return;

m_Transform.position = new Vector3(Mathf.Clamp(m_Transform.position.x, -limitX, limitX),
m_Transform.position.y,
Mathf.Clamp(m_Transform.position.z, -limitZ, limitZ));
}

private void HeightCalculation()
{
float distanceToGround = DistanceToGround();
if (useScrollwheelZooming)
zoomPos += ScrollWheel * Time.deltaTime * scrollWheelZoomingSensitivity;


zoomPos = Mathf.Clamp01(zoomPos);

float targetHeight = Mathf.Lerp(minHeight, maxHeight, zoomPos);
float difference = 0;

if (distanceToGround != targetHeight)
difference = targetHeight - distanceToGround;

m_Transform.position = Vector3.Lerp(m_Transform.position,
new Vector3(m_Transform.position.x, targetHeight + difference, m_Transform.position.z), Time.deltaTime * heightDampening);
}
private float DistanceToGround()
{
Ray ray = new Ray(m_Transform.position, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, groundMask.value))
return (hit.point - m_Transform.position).magnitude;

return 0f;
}



}

最佳答案

将第 25 行的变量 zoomPos 的值设置为 1 或 0,具体取决于您希望它处于最小高度还是最大高度,您可以在 Start() 或仅在变量声明处。

这是因为在 HeightCalculation 函数中,它根据 zoomPos 的值将高度设置为介于最小值和最大值之间的某个值,当您使用滚轮时,该值会发生变化变焦也是如此。在运行游戏之前更改 zoomPos 可让您有效地设置默认起始高度。

关于c# - Unity相机起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46163008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com