为什么当我定义单个调用时该值没有改变?
Why does the value not change when I define a single call?
为什么定义单个调用时,值没有变化?
if (Input.touchCount == 1)
{
Touch screentouch = Input.GetTouch(0);
var j1 = joint1.transform.position;
var j2 = joint2.transform.position;
if (screentouch.phase == TouchPhase.Moved)
{
if (distance)
{
j1 = j2; // no work???
}
}
}
不过我用下面一个是find。
j1 = j2; replace to
joint1.transform.position = joint2.transform.position; is ok
如果我想用 var j1 替换 long,我该怎么办 joint1.transform.position;
谢谢
问题是您没有应用作业。当你打电话时:
var j1 = joint1.transform.position;
var j2 = joint2.transform.position;
您正在获取 joint1
和 joint2
的当前位置。当您分配 j1 = j2
时,您只是在更改 j1
的 Vector3
值,您实际上并没有更改位置。
如果您想要 joint1
实际移动,那么您需要设置它的位置:
joint1.transform.position = j1;
仅当 b
引用 a
时,更改变量 a
才能更改另一个变量 b
。
这在 C++ 中是可能的
int x = 10;
int &ref = x // ref is referenced to x
ref = 20; // ref is assigned a new value and hence x is also modified
cout << x; // prints 20
在 C# 中没有针对此的解决方案,因为在 C# 中使用指针或引用(如果存在)被认为是不安全的。即使您尝试在 C# 中使用指针,您也会收到一条错误消息:
error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
但是,在你的问题中,要更新 joint1.transform.position
,因为你不能在 C# 中使用引用,你可以只写
joint1.transform.position = j2 // assigns joint2.transform.position to joint1.transform.position
为什么定义单个调用时,值没有变化?
if (Input.touchCount == 1)
{
Touch screentouch = Input.GetTouch(0);
var j1 = joint1.transform.position;
var j2 = joint2.transform.position;
if (screentouch.phase == TouchPhase.Moved)
{
if (distance)
{
j1 = j2; // no work???
}
}
}
不过我用下面一个是find。
j1 = j2; replace to
joint1.transform.position = joint2.transform.position; is ok
如果我想用 var j1 替换 long,我该怎么办 joint1.transform.position; 谢谢
问题是您没有应用作业。当你打电话时:
var j1 = joint1.transform.position;
var j2 = joint2.transform.position;
您正在获取 joint1
和 joint2
的当前位置。当您分配 j1 = j2
时,您只是在更改 j1
的 Vector3
值,您实际上并没有更改位置。
如果您想要 joint1
实际移动,那么您需要设置它的位置:
joint1.transform.position = j1;
仅当 b
引用 a
时,更改变量 a
才能更改另一个变量 b
。
这在 C++ 中是可能的
int x = 10;
int &ref = x // ref is referenced to x
ref = 20; // ref is assigned a new value and hence x is also modified
cout << x; // prints 20
在 C# 中没有针对此的解决方案,因为在 C# 中使用指针或引用(如果存在)被认为是不安全的。即使您尝试在 C# 中使用指针,您也会收到一条错误消息:
error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
但是,在你的问题中,要更新 joint1.transform.position
,因为你不能在 C# 中使用引用,你可以只写
joint1.transform.position = j2 // assigns joint2.transform.position to joint1.transform.position