简体   繁体   中英

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));
    }

}

}

I get an Object reference not set to an instance of an object error. I get this error on line "this.XForm_Parent.rotation=Quaternion.Lerp(this.XForm_Parent.rotation, QT, Time.deltaTime * OrbitSpeed);". Hope someone can help me been struggling on this for a long time now.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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