【发布时间】:2018-06-28 13:10:07
【问题描述】:
目前,我遇到了通过 c# 脚本统一更改主摄像机位置的问题。我正在尝试随着火箭改变相机的位置。所以理想情况下,火箭应该在屏幕左侧稍微偏移。它应该在玩游戏时跟随。
这是我的相机脚本,当我按下播放键时,它应该跟随目标“TheRocket”。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
Camera mainCam;
public Transform target;
public float distance = 15.0f;
public float cameraY = 1.0f;
// Update is called once per frame
void Update () {
Vector3 pos = transform.position;
pos.z = target.position.z - distance;
pos.x = target.position.x + 18;
}
void LateUpdate () {
mainCam = Camera.main;
Vector3 temp = mainCam.transform.position;
temp.y = cameraY;
}
}
我的控件类如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controls : MonoBehaviour {
public float speed = 8.0f;
public float jumpspeed = 20.5f;
public float gravity = 20.0f;
Vector3 moveDirection = Vector3.zero;
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if(controller.isGrounded) {
moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal") + 3);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if(Input.GetButtonDown("Jump")) {
moveDirection.y += jumpspeed;
}else{
if(Input.GetButtonDown("Jump")){
moveDirection.y += jumpspeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
老实说,我不知道我在这里做错了什么。我用这个教程作为参考:
https://www.youtube.com/watch?v=O0CxA3I2eYQ
希望有人能找出问题所在!
截图
场景
场景播放(应跟随火箭并被放大)
主摄像头检查器视图
火箭队
【问题讨论】:
-
我将首先描述您遇到的实际问题......
-
@RobIII 当然。我加了一些煮沸