我建了個空物體把主攝像機放在了這個空物體之下然后我給它加了一個用input監測用transform移動的腳本,但是我運行起來后他給我出現了很多報錯,有大佬這道這是什么出問題了嗎
報錯截圖:

移動腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
Transform _transform;
// Use this for initialization
void Start () {
_transform= gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
MonitorUserSelectMoveGameObjet();
}
/// <summary>
/// 使用iput監測用戶輸入在使用transform移動游戲物體
/// </summary>
private void MonitorUserSelectMoveGameObjet()
{
if (Input.GetKey(KeyCode.W))
{
_transform.Translate(_transform.position + Vector3.forward, Space.World);
}
if (Input.GetKey(KeyCode.A))
{
_transform.Translate(_transform.position + Vector3.left, Space.World);
}
if (Input.GetKey(KeyCode.S))
{
_transform.Translate(_transform.position + Vector3.back, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
_transform.Translate(_transform.position + Vector3.right, Space.World);
}
}
}
uj5u.com熱心網友回復:
錯誤提示很明顯 相機的坐標超出前螢屏你可以了解一下 相機坐標系和世界坐標系的區別
我覺得你是想要這樣的代碼?
float speed=1f;
//
_transform.Translate(Vector3.up* speed * Input.GetAxis("Vertical") * Time.deltaTime);
_transform.Translate(Vector3.right* speed * Input.GetAxis("Horizontal") * Time.deltaTime);
//WASD 可以參考一下 類似
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/22250.html
標籤:Unity3D
