弾を撃つプログラム
Y座標の移動値を渡すことで斜めにも打てるようにした。
BulletInst(0);
BulletInst(1);
BulletInst(-1);
Rigidbody2Dではtransform.forwardはZ値が無いため使用できないらしい。
void BulletInst(float y) { //作成 GameObject obj = Instantiate(bullet, transform.position + new Vector3(0f, 0.25f, 0f), transform.rotation); //rigidbody2Dコンポーネントを取得 Rigidbody2D rb = obj.GetComponent<Rigidbody2D>(); //画像の向きをユニティちゃんに合わせる Vector2 temp = transform.localScale; temp.x = transform.localScale.x; obj.transform.localScale = temp; //ユニティちゃんの向いている向きに弾を飛ばす float spd = speed * transform.localScale.x + rigidbody2D.velocity.x; rb.velocity = new Vector2( spd, rigidbody2D.velocity.y + y); //5秒後に消滅 Destroy(obj, 5); }
360度 敵を生成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
public class enemyInst : MonoBehaviour
{
float time;
//生み出す敵のオブジェクト
public GameObject EnemyObj;
//プレイヤーのオブジェクト
GameObject PlayerObj;
//半径
private float _radius = 10;
// Start is called before the first frame update
void Start()
{
//プレイヤーを探す
PlayerObj = GameObject.Find("Player");
}
// Update is called once per frame
void Update()
{
//時間を計測 1で1秒
time += Time.deltaTime;
if (time >= 1) {
//リセット
time = 0;
//敵を生成
GameObject obj = Instantiate(EnemyObj);
//初期位置
Vector3 childPostion = transform.position;
//何度か
float angle = Random.Range( 0, 360) * Mathf.Deg2Rad;
//360度の位置を設定
childPostion.x += _radius * Mathf.Cos(angle);
childPostion.y = transform.position.y;
childPostion.z += _radius * Mathf.Sin(angle);
//位置を入れる
obj.transform.position = childPostion;
//自機の方を向く
obj.transform.LookAt(PlayerObj.transform);
//リジットボディの取得
Rigidbody rb = obj.GetComponent<Rigidbody>();
//吹き飛ばす
rb.AddForce(obj.transform.forward * 5.0f, ForceMode.Impulse);
}
}
}
マウスの方を向く
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lookat : MonoBehaviour {
Plane plane = new Plane();
float distance = 0;
void Update()
{
// カメラとマウスの位置を元にRayを準備
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// プレイヤーの高さにPlaneを更新して、カメラの情報を元に地面判定して距離を取得
plane.SetNormalAndPosition(Vector3.up, transform.localPosition);
if (plane.Raycast(ray, out distance)) {
// 距離を元に交点を算出して、交点の方を向く
var lookPoint = ray.GetPoint(distance);
transform.LookAt(lookPoint);
}
}
}
当たったものを消す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tama : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy") {
Destroy(collision.gameObject);
//Destroy(gameObject);
}
}
}
弾を撃つ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fire : MonoBehaviour
{
public GameObject Tama;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1")) {
//オブジェクトの作成
GameObject Obj = Instantiate(Tama);
//座標を合わせる
Obj.transform.position = gameObject.transform.position;
//向きをあわせる
Obj.transform.forward = gameObject.transform.forward;
//リジットボディの取得
Rigidbody rb = Obj.GetComponent<Rigidbody>();
//吹き飛ばす
rb.AddForce(transform.forward * 50.0f, ForceMode.Impulse);
//自動で消す
Destroy(Obj, 5.0f);
}
}
}
オブジェクトの移動と回転
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float speed = 10.0f;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//横の入力 -1 ~ 0 ~ 1
float h = Input.GetAxis("Horizontal");
//縦の入力 -1 ~ 0 ~ 1
float v = Input.GetAxis("Vertical");
//前後に移動
rb.velocity = transform.forward * v * speed;
//rb.velocity = transform.forward * v * speed;
//rb.velocity += transform.right * h * speed;
//回転
transform.Rotate(0, h, 0);
}
}
【Unity】 カメラをマウスで回転させる 上下に制限を付ける
using System.Collections; using System.Collections.Generic; using UnityEngine; //カメラにくっ付けるプログラム //参考にしたURL //http://mslgt.hatenablog.com/entry/2017/02/18/020630 public class CameraControll : MonoBehaviour { //マウスの座標を保存する用 private Vector3 lastMousePosition; //回転の計算用 private Vector3 newAngle = new Vector3(0, 0, 0); //Update 毎フレーム呼ばれる命令 60fpsなら1秒間に大体60回(必ずではない) private void Update () { //マウスがクリックされた最初を検知 if (Input.GetMouseButtonDown(0)) { // マウスクリック開始(マウスダウン)時にカメラの角度を保持(Z軸には回転させないため). newAngle = transform.localEulerAngles; lastMousePosition = Input.mousePosition; } else if (Input.GetMouseButton(0)) { //クリックされ続けている // マウスの移動量分カメラを回転させる. newAngle.y -= (Input.mousePosition.x - lastMousePosition.x) * 0.1f; newAngle.x -= (Input.mousePosition.y - lastMousePosition.y) * 0.1f; // 上下の移動に制限をかける if ( newAngle.x <= -30.0f ) { newAngle.x = -30.0f; } if ( newAngle.x >= 30.0f ) { newAngle.x = 30.0f; } // 新しい回転の数値を代入 gameObject.transform.localEulerAngles = newAngle; lastMousePosition = Input.mousePosition; } } }