当我尝试附加GravityBody脚本和GravityAttractor脚本时,会弹出这3个错误:
资产/脚本/GravityAttractor.cs(15,42):错误CS1502:最佳 重载方法匹配 `UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3, UnityEngine.Vector3)'有一些无效的参数
Assets / Scripts / GravityAttractor.cs(15,65):错误CS1503:参数
#2' cannot convert
float表达式键入`UnityEngine.Vector3'资产/脚本/PlayerMovement.cs(21,29):错误CS1061:类型
UnityEngine.Rigidbody' does not contain a definition for
MoveDirection'和扩展方法MoveDirection' of type
UnityEngine.Rigidbody'均未找到。您是否缺少大会 参考?
这是脚本的代码;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityBody : MonoBehaviour {
// Use this for initialization
public GravityAttractor attractor;
private Transform myTransform;
void Start () {
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
myTransform = transform;
}
// Update is called once per frame
void FixedUpdate () {
if (attractor){
attractor.Attract(myTransform);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityAttractor : MonoBehaviour {
public float gravity = -10;
public void Attract(Transform myTransform) {
Vector3 gravityUp = (myTransform.position - transform.position).normalized;
Vector3 localUp = myTransform.up;
myTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation(localUp,gravity) * myTransform.rotation;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,targetRotation,(50f * Time.deltaTime));
}
}
答案 0 :(得分:1)
Assets / Scripts / GravityAttractor.cs(15,42):错误CS1502:“ UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3,UnityEngine.Vector3)”的最佳重载方法匹配具有一些无效参数
这很简单-只需阅读错误消息即可。 “ Quaternion.FromToRotation(UnityEngine.Vector3,UnityEngine.Vector3)'有一些无效的参数”表示您的代码对该方法有一些不正确的参数(特别是Quaternion.FromToRotation(您的参数)。您可以看到预期的参数here, at the Unity docs(靠近顶部)。您可以看到该方法带有两个Vector3对象。我猜您可能是不小心写了gravity
而不是gravityUp
吗? / p>
资产/脚本/GravityAttractor.cs(15,65):错误CS1503:参数2' 无法将float'表达式转换为类型'UnityEngine.Vector3'
同样的问题。编译器告诉您,它尝试调用Quaternion.FromToRotation
方法,但是无法将gravity
浮点数更改为Vector3(这是所需的cuz)
资产/脚本/PlayerMovement.cs(21,29):错误CS1061:类型UnityEngine.Rigidbody'不包含以下定义: “ MoveDirection”且没有类型的扩展方法“ MoveDirection” 可以找到UnityEngine.Rigidbody'。您是否缺少装配参考?
好吧,您没有显示此代码,但是该错误意味着您在代码中的某个地方,.MoveDirection
上的某个地方RigidBody
变量上有.MoveDirection
。再次,错误消息显示“ UnityEngine.Rigidbody'不包含
MoveDirection'“-就是这个意思。您在错误的变量上调用了c1 c2 c3 c4 c5 c6
C875 DOID_3263 1 9.65E-18 1 unknown
C783 DOID_4064 1 4.80E-17 1 unknown
C372 DOID_0050084 0.996 0.00429 0.996 unknown
C43 DOID_936 0.0457 0.954 0.954 known