簡體   English   中英

如何在 Unity 中更改播放器的 y 比例?

[英]How do I change the y scale of my player in Unity?

當我嘗試此代碼時,它給了我以下錯誤:“無法修改 Transform.localRotation,因為它不是變量。”

public Transform Player;
public float speed = 12f;
bool isGrounded;

if (Input.GetKey("left ctrl") && isGrounded)
{
     speed = 6f;

     Player.localScale.y = 0.5f;
}
else
{
     speed = 12f;
     Player.localScale.y = 1f;
}

Transform.localScaleVector3 屬性Transform.localRotationQuaternion 屬性,它們都是struct值類型屬性。

你不能只改變它們的一個字段,因為它只會改變屬性的 getter 返回的struct中的一個字段,但永遠不會用新值調用屬性的 setter。

您寧願必須重新分配整個值,例如

// Store in a temporary variable
var scale = Player.localScale;
// Now in a variable (or field) you CAN change the field values
scale.y = 0.5f;
// Assign back to the property
Player.localScale = scale;

另請參閱c# 中的結構和屬性問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM