unity3d - Unity 2018 相机跟随对象但用箭头键平移
问题描述
我对 Unity 很陌生(几天前开始)。
我正在尝试创建相机始终跟随滚动球的功能,但相机也应由箭头键控制,同时仍专注于主要对象。
我有以下代码跟随实际对象:
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public Transform player;
private Vector3 offset = new Vector3(0, 3, -5);
// Update is called once per frame
void Update() {
transform.position = player.position + offset;
}
}
上面的代码完美运行,它在移动时跟随对象。
但我尝试了以下方法来移动相机:
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public Transform player;
public GameObject MainCamera;
public Vector3 offset = new Vector3(0, 3, -5);
private Vector3 RotateOffset;
public float rotateJump = 1f;
// Update is called once per frame
void Update() {
//Rotate Camera 90 degrees Left
if (Input.GetKey("left")) {
RotateOffset = new Vector3(transform.position.x - rotateJump, 0, transform.position.z - rotateJump);
transform.position = player.position + RotateOffset;
}
//Rotate Camera 90 degrees Right
if (Input.GetKey("right")) {
RotateOffset = new Vector3(transform.position.x + rotateJump, 0, transform.position.z + rotateJump);
transform.position = player.position + RotateOffset;
}
}
}
但这只会将相机沿直线从位置 A 移动到 B,如下所示,不会旋转:
我怎样才能让相机跟随物体并仍然由用户控制?
提前致谢!
我在板上移动对象的代码如下所示:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float MovementForce = 200f;
void FixedUpdate() {
//Move Forward
if (Input.GetKey("w")) {
rb.AddForce(0, 0, MovementForce * Time.deltaTime);
}
//Move Left
if (Input.GetKey("a")) {
rb.AddForce(-MovementForce * Time.deltaTime, 0, 0);
}
//Move Back
if (Input.GetKey("s")) {
rb.AddForce(0, 0, - MovementForce * Time.deltaTime);
}
//Move Right
if (Input.GetKey("d")) {
rb.AddForce(MovementForce * Time.deltaTime, 0, 0);
}
}
}
编辑:
我将 FollowPlayer 脚本更新为如下所示:
using UnityEngine;
public class FollowPlayer : MonoBehaviour {
public Transform player;
private Vector3 offset = new Vector3(0, 3, -5);
private float RotateVal = 2f;
private Vector3 VPanAmount = new Vector3(2, 0, 0);
private Vector3 HPanAmount = new Vector3(0, 2, 0);
private Vector3 VRotatoinOffset;
private Vector3 HRotatoinOffset;
private void Start() {
}
// Update is called once per frame
void Update() {
if (Input.GetKey("left")) {
VRotatoinOffset = player.position + offset - VPanAmount;
transform.Rotate(0, -RotateVal, 0);
}
if (Input.GetKey("right")) {
VRotatoinOffset = player.position + offset + VPanAmount;
transform.Rotate(0, RotateVal, 0);
}
if (Input.GetKey("up")) {
HRotatoinOffset = player.position + offset + HPanAmount;
transform.Rotate(-RotateVal, 0, 0);
}
if (Input.GetKey("down")) {
HRotatoinOffset = player.position + offset + HPanAmount;
transform.Rotate(RotateVal, 0, 0);
}
transform.position = player.position + offset - VRotatoinOffset - HRotatoinOffset;
}
}
相机随着物体的移动而旋转和移动,但相机的焦点不会停留在物体上,相机会跳到比赛场地的下方。
解决方案
推荐阅读
- sql - 从不同的表问题中选择列值
- angular - Angular 6 使用延迟加载访问父组件的子组件时出现意外行为
- javascript - 如何识别域名是http还是https?
- powershell - 在 Azure 中更改 Key Vault 的密码
- neo4j - 如果不存在则在neo4j中创建关系,如果存在则更新属性值
- reactjs - React-Router-V4 历史推送后从 React 组件获取数据
- maven - 使用 docker 容器的 Spring Boot 集成测试多模块 maven 应用程序
- c# - 构建字符串时如何保持上一行的缩进
- python - Django Web 服务器启动但不请求任何内容
- python - GT521F52 指纹传感器库