Unityでジャンクゲームを作っているっぽい

会社帰りにGPD PocketにUnity入れてゲーム作ってます

【2DのTest】03_左右移動

左右キーで左右移動したい

今回の参考サイト

akiblog10.com

player_move.csを新規作成

とてもシンプルなスクリプトですね

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player_move : MonoBehaviour{
	public float speed;				//速度
	private Rigidbody2D rb = null;

    void Start(){
		rb = GetComponent<Rigidbody2D>();	//Rigidbody2D取得
	}

	//物理演算用
	void FixedUpdate(){
		float horizontalKey = Input.GetAxis("Horizontal");

		//右入力
		if(horizontalKey > 0){
			rb.velocity = new Vector2(speed, rb.velocity.y);
		}
		//左入力
		else if(horizontalKey < 0){
			rb.velocity = new Vector2(-speed, rb.velocity.y);
		}
		//無入力
		else{
			rb.velocity = Vector2.zero;
		}
	}
}

スクリプトをplayerにアサイ

とりあえず速度には2を入れてます
f:id:mekatamatama:20201204214614p:plain

動きました~
f:id:mekatamatama:20201204214751g:plain:h300

でも、重力が変な感じなのはジャンプ実装時にで解決予定