设置行星重力时的Unity/C#错误
当我尝试附加GravityBody脚本和GravityAtractor脚本时,会弹出以下3个错误: Assets/Scripts/gravityatractor.cs(15,42):错误CS1502:最佳 重载方法匹配 `UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3, UnityEngine.Vector3)具有一些无效参数 Assets/Scripts/gravityatractor.cs(15,65):错误CS1503:参数设置行星重力时的Unity/C#错误,c#,unity3d,C#,Unity3d,当我尝试附加GravityBody脚本和GravityAtractor脚本时,会弹出以下3个错误: Assets/Scripts/gravityatractor.cs(15,42):错误CS1502:最佳 重载方法匹配 `UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3, UnityEngine.Vector3)具有一些无效参数 Assets/Scripts/gravityatractor.cs(15,65):错误CS1503:参
#2'
无法将
float'表达式转换为类型“UnityEngine.Vector3”
Assets/Scripts/PlayerMovement.cs(21,29):错误CS1061:Type
UnityEngine.Rigidbody'不包含
MoveDirection'和类型的无扩展方法MoveDirection'
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));
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类重力体:单行为{
//用于初始化
公共引力吸引子;
私有变换;
无效开始(){
GetComponent().useGravity=false;
GetComponent().constraints=RigidByConstraints.FriezeRotation;
myTransform=转换;
}
//每帧调用一次更新
无效固定更新(){
if(吸引子){
吸引(myTransform);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类:单一行为{
公众浮标重力=-10;
公共空间吸引(转换myTransform){
Vector3 gravityUp=(myTransform.position-transform.position).normalized;
Vector3 localUp=myTransform.up;
myTransform.GetComponent().AddForce(重力)Yup*gravity;
四元数targetRotation=四元数。从旋转(localUp,重力)*myTransform.rotation;
myTransform.rotation=Quaternion.Slerp(myTransform.rotation,targetRotation,(50f*时间增量));
}
}
Assets/Scripts/GravityAttractor.cs(15,42):错误CS1502:“UnityEngine.Quaternion.FromToRotation(UnityEngine.Vector3,UnityEngine.Vector3)”的最佳重载方法匹配具有一些无效参数
这是一个相当直接的-只要阅读错误消息。“Quaternion.FromToRotation(UnityEngine.Vector3,UnityEngine.Vector3)”具有一些无效参数”表示您的代码具有该方法的一些不正确参数(特别是,Quaternion.FromToRotation(您的参数)。您可以看到预期参数(请看顶部附近)。您可以看到该方法使用两个Vector3对象。我猜您可能只是意外地编写了gravity
,而不是gravityUp
Assets/Scripts/gravityatractor.cs(15,65):错误CS1503:参数#2'
无法将“float”表达式转换为类型“UnityEngine.Vector3”
同样的问题。编译器告诉您它试图调用四元数.FromToRotation
方法,但它无法将重力
浮点更改为矢量3(因为它需要这样做)
Assets/Scripts/PlayerMovement.cs(21,29):错误CS1061:类型UnityEngine.Rigidbody'不包含的定义
类型的MoveDirection'和无扩展方法MoveDirection'
找不到UnityEngine.Rigidbody“”。是否缺少程序集引用
嗯,您没有显示此代码,但该错误意味着在代码的某个地方,您在刚体
变量的某个地方有.MoveDirection
。同样,错误消息说“UnityEngine.RigidBody”不包含
“移动方向”-这正是它的意思。你在错误的变量上调用了
.MoveDirection
,那么你的问题是什么?你不明白错误消息的意思吗?我无法修复它,而且我在internet上找不到解决方案,我的代码也因为这些错误而无法运行。你能帮我解决这些错误吗?