我第一次在unity做了一个小游戏,但是现在我在keydown事件中缩放“玩家”时遇到了一个小问题。
这是我的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;
}
}
答案 0 :(得分:2)
你必须在关键向下和向上
上设置游戏对象l ocal scalevoid Update(){
if (isOnGround == true && Input.GetKeyDown (KeyCode.DownArrow))
gameObject.transform.localScale = new Vector3(2.0f, 2.0f, 1.0f);
if (Input.GetKeyUp (KeyCode.DownArrow))
gameObject.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}