やまめも

【 ゲーム開発・日々のことについて 】

オブジェクトを移動

方向キーで物体を移動

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

	Vector3 movement;
	Rigidbody playerRigidbody;
	public float speed = 6f;

	void Start () {
		playerRigidbody = GetComponent<Rigidbody>();
	}
	void FixedUpdate(){
		float h = Input.GetAxisRaw ("Horizontal");
		float v = Input.GetAxisRaw ("Vertical");

		Move(h,v);

	}
	void Move (float h, float v){
		movement = new Vector3(h, 0f, v);
		movement = movement.normalized * speed * Time.deltaTime;

		playerRigidbody.MovePosition (transform.position + movement);

	}
}

例2
AddForceを使う

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	private GameObject _charactor;

	void Start () {
	this._charactor = GameObject.Find ("charactor");
	}

	void Update () {
		// 方向キーの入力を常に取得する。
		float horizontal = Input.GetAxis("Horizontal");
		this._charactor.rigidbody2D.AddForce(Vector2.right * 10f * horizontal);
	}
}

座標を変えることによって移動する。

	private bool _isLeftMove;
	private float _movePoint = 0.1f;

	void Start () {
		this._isLeftMove = true;

	}
	void Update () {

		var position = this.transform.position;
		if( this._isLeftMove = true){
			position.x -= this._movePoint;
		}
		else{
			position.x += this._movePoint;
		}
		transform.position = position;
	
	}