我正在研究某种用于教育目的的寺庙游戏项目。我创建了一个脚本,该脚本应该从一点点高处关注字符,然后将其位置更改为字符。就像在太阳穴运行的时候,相机在游戏脚本时慢慢向玩家发出韵律。这是我的剧本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMotor : MonoBehaviour {
private Vector3 ofset;
private Vector3 characterposition;
private float transition =0.0f;
private float animationduration = 2.0f;
private Vector3 animationoffset = new Vector3(0,2f,2f);
void Start () {
ofset = GameObject.FindObjectOfType<CharacterController> ().transform.position - this.transform.position;
}
// Update is called once per frame
void Update () {
characterposition = GameObject.FindObjectOfType<CharacterController> ().transform.position - ofset;
characterposition.x = 1.3f;
characterposition.y = Mathf.Clamp (characterposition.y, 3f, 5f);
if (transition > 1.0f) {
this.transform.position = characterposition;
} else {
this.transform.position = Vector3.Lerp (characterposition + animationoffset, characterposition, transition);
transition = Time.deltaTime* 1 / animationduration;
this.transform.LookAt (characterposition, Vector3.up);
}
}
}
答案 0 :(得分:1)
以下行错误
this.transform.LookAt (characterposition, Vector3.up);
LookAt()方法需要Transform类型参数,而不是Vector3。尝试使用这些
private Vector3 ofset;
private Vector3 characterposition;
private CharacterController characterCon;
private float transition =0.0f;
private float animationduration = 2.0f;
private Vector3 animationoffset = new Vector3(0,2f,2f);
void Start () {
ofset = GameObject.FindObjectOfType<CharacterController> ().transform.position - this.transform.position;
characterCon = GameObject.FindObjectOfType<CharacterController> ().GetComponent<CharacterController>();
}
void Update () {
characterposition = characterCon.transform.position - ofset;
characterposition.x = 1.3f;
characterposition.y = Mathf.Clamp (characterposition.y, 3f, 5f);
if (transition > 1.0f) {
this.transform.position = characterposition;
} else {
this.transform.position = Vector3.Lerp (characterposition + animationoffset, characterposition, transition);
transition = Time.deltaTime * 1 / animationduration;
}
this.transform.LookAt (characterCon.transform, Vector3.up);
}
此外,您在每一帧调用FindObjectOfType方法都会导致性能下降。 请查看以下链接LookAt
答案 1 :(得分:0)
您已接近但您的过渡值需要不断增加,以使您的Lerp正常工作。尝试更改此行:
transition = Time.deltaTime* 1 / animationduration;
到
transition += Time.deltaTime* 1 / animationduration;