0
我第一次在unity做了一個小遊戲,但是現在我面臨着在一個keydown事件中縮放「玩家」的小問題。Unity 2D遊戲Scale按鍵不起作用
這裏是我的c#腳本代碼:
void Update(){
//if we are on the ground and the space bar was pressed, change our ground state and add an upward force
if (isOnGround == true && Input.GetKeyDown (KeyCode.UpArrow)) {
anim.SetBool("Ground",false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
if (isOnGround == true && Input.GetKeyDown (KeyCode.DownArrow))
{
//HERE I NEED THE SCALE.
//player.transform.localScale = new Vector3(transform.localScale.x 1, transform.localScale.y 1, transform.localScale.z 1);
}
}
我的問題是,我怎樣才能改變「玩家」的規模上Input.GetKeyDown (KeyCode.DownArrow)
,然後再返回到原來的規模大小釋放。
我很樂意幫助解決這個問題。
非常感謝您的時間。
更新:
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
public GameObject player;
public GameObject sprite;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//to check ground and to have a jumpforce we can change in the editor
bool grounded = true;
public Transform groundCheck;
public float groundRadius = 1f;
public LayerMask whatIsGround;
public float jumpForce = 300f;
private bool isOnGround = false;
void OnCollisionEnter2D(Collision2D collision) {
isOnGround = true;
}
void OnCollisionExit2D(Collision2D collision) {
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
isOnGround = false;
}
// Use this for initialization
void Start() {
//set anim to our animator
anim = GetComponent <Animator>();
}
void FixedUpdate() {
//set our vSpeed
//set our grounded bool
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
//set ground in our Animator to match grounded
anim.SetBool ("Ground", grounded);
float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);
//set our speed
anim.SetFloat ("Speed",Mathf.Abs (move));
//if we are moving left but not facing left flip, and vice versa
if (move > 0 && !facingLeft) {
Flip();
} else if (move < 0 && facingLeft) {
Flip();
}
}
void Update(){
if ((isOnGround == true && Input.GetKeyDown (KeyCode.UpArrow)) || (isOnGround == true && Input.GetKeyDown (KeyCode.Space))) {
anim.SetBool("Ground",false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
if (isOnGround == true && Input.GetKeyDown (KeyCode.DownArrow))
{
}
}
//flip if needed
void Flip(){
facingLeft = !facingLeft;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
player.transform.localScale =新的Vector3( 1.0f,1.0f,1.0f);返回錯誤 UnassignedReferenceException:RobotController的變量播放器尚未分配。 您可能需要在檢查器中分配RobotController腳本的播放器變量。 RobotController.Update()(在Assets/Script/RobotController.cs:70) – 2014-11-06 11:26:07
是否有分配給玩家的任何東西 – 2014-11-06 11:28:08
我已經用整個腳本更新了帖子。 – 2014-11-06 11:28:56