LoginSignup
0
0

More than 5 years have passed since last update.

アイテムを拾うゲームの作成(コードのみ)No.1

Posted at

こちらのコードは以下のサイトのコードのみを書いています.
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キーでのジャンプ,矢印キーを押したときにプレイヤーキャラが移動方向を向くように設定.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0