繁体   English   中英

Unity试图对相机轨道控制器

[英]Unity trying to camera orbit controller

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

public class camMouseLook : MonoBehaviour {

protected Transform XForm_camera;
protected Transform XForm_Parent;

protected Vector3 LocalRotation;
protected float CameraDistance = 10f;

public float MouseSensitivity = 4f;
public float ScrollSensitivity = 2f;
public float OrbitSpeed = 10f;
public float ScrollSpeed = 6f;

public bool CameraDisabled = false;



void Start () {
    this.XForm_camera = this.transform;
    this.XForm_Parent = this.transform.parent;

}

// Update is called once per frame
void LateUpdate () {
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        CameraDisabled = !CameraDisabled;
    }
    if (!CameraDisabled)
    {
        if(Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
        {
            LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
            LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;

            LocalRotation.y = Mathf.Clamp(LocalRotation.y, 0f, 90f);
        }
       if(Input.GetAxis("Mouse ScrollWheel") != 0f)
        {
            float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * 
    ScrollSensitivity;

            ScrollAmount *= (this.CameraDistance * 0.3f);
            this.CameraDistance += ScrollAmount * -1f;
            this.CameraDistance = Mathf.Clamp(this.CameraDistance, 1.5f, 100f);
        }

    }
    Quaternion QT = Quaternion.Euler(LocalRotation.y, LocalRotation.x, 0);
    this.XForm_Parent.rotation = Quaternion.Lerp(this.XForm_Parent.rotation, QT, Time.deltaTime * OrbitSpeed);

    if (this.XForm_camera.localPosition.z != this.CameraDistance * -1f)
    {
        this.XForm_camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(this.XForm_camera.localPosition.z, this.CameraDistance * -1f, Time.deltaTime * ScrollSpeed));
    }

}

}

我得到未设置为对象错误实例的对象引用。 我在“ this.XForm_Parent.rotation = Quaternion.Lerp(this.XForm_Parent.rotation,QT,Time.deltaTime * OrbitSpeed);”行上收到此错误。 希望有人可以帮助我在很长一段时间内一直为此奋斗。

您只需要将gameObject附加到父对象,因为您要告诉this.XForm_Parent = this.transform.parent,并且gameObject上没有父对象;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM