所以我正在尝试使用Unity 5 FPS控制器做一些不同的事情,并将我从这里获得的陀螺仪相机脚本(http://forum.unity3d.com/threads/sharing-gyroscope-camera-script-ios-tested.241825/)附加到FirstPersonCharacter相机(在评论出每个与鼠标相关的代码后)在FirstPersonController.cs中)。到目前为止,FirstPersonCharacter根据手机的动作环顾四周,但是当玩家开始移动时会出现问题。
玩家(FPSController)和相机(FirstPersonCharacter)的移动被断开,玩家移动和扫射,而不管FirstPersonCharacter的变换。这是因为只有FirstPersonCharacter的变换受到陀螺仪控制的影响,而不受FPSController的影响。
我尝试通过向FirstPersonController.cs添加控件来实现修复,其中FPSController仅沿y轴旋转,并且可以工作,但仅适用于父FPSController。它拧紧了FirstPersonCharacter,使其在直视时向前滚动,在向上或向下看时偏转,并在FPSController转动时向侧面俯仰。
我希望转动父母的行为会影响孩子的行为,而且确实如此,但只是以一种可怕的方式。任何人都有解决方案或想法来解决这个问题吗?
答案 0 :(得分:0)
对于我的播放器控制器,我总是使用此代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class SC_FPSController : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
如果你想和播放器一起移动相机,你必须把相机作为播放器的子文件夹。
希望能帮到你!