2014-11-06 2 views
0

Я сделал небольшую игру в в первый раз, но теперь у меня возникла небольшая проблема с масштабированием «игрока» на событии keydown.Unity 2D игра Масштаб игрока по нажатию клавиши не работает

Вот мой код сценария:

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) И снова к исходному размеру шкалы при отпускании.

Я хотел бы помочь с этой проблемой.

Спасибо большое за ваше время.

Update:

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; 
} 
} 

ответ

2

вы должны установить геймобжекты л Öçal масштаб по ключевым вниз и до

void 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); 


    } 
+0

player.transform.localScale = новый Vector3 (1.0f, 1.0f, 1.0f); Возвращает ошибку UnassignedReferenceException: переменный игрок RobotController не назначен. Вероятно, вам нужно назначить переменную игрока скрипта RobotController в инспекторе. RobotController.Update() (в Assets/Script/RobotController.cs: 70) –

+0

есть что-то назначенное игроку –

+0

У меня есть сообщение со всем скриптом. –

Смежные вопросы