こちらのコードは以下のサイトのコードのみを書いています.
pickup-gameの作成 No.1
PlayerController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerController:MonoBehaviour{
public float speed;
public float flag=1000f;
private Rigidbody2D rb2d;
void Start(){
rb2d =GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
float x = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(moveHorizontal,moveVertical);
rb2d.AddForce(movement * speed);
Vector3 scale = transform.localScale;
if(x >= 0){
scale.x = -1;
}else{
scale.x = 1;
}
transform.localScale = scale;
}
void Update(){
if(Input.GetKeyDown("space")){
rb2d.AddForce(Vector2.up*flag);
}
}
void OnTriggerEnter2D(Collider2D other){
if(other.gameObject.CompareTag("PickUp")){
other.gameObject.SetActive(false);
}
}
}
プレイヤーの矢印キーによる操作,Spaceキーでのジャンプ,矢印キーを押したときにプレイヤーキャラが移動方向を向くように設定.