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