前提・実現したいこと
Unity2Dでブロック崩しを制作しています。
ボールが当たったら物理反射を実装したいです。
発生している問題・エラーメッセージ
ボールがブロックに当たったら消える処理はUnityの機能を使用して実装できたのですが、その処理はただ消えるだけで物理反射をしてくれません。
エラーメッセージ
該当のソースコード
ボール
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private float speed = 1000.0f;
private int HP = 9;
public Rigidbody2D rd;
private float px;
private float py;
// object gameObject Obstacle;
//private int HP =;
// Start is called before the first frame update
private void Start()
{
//物理演算コンポーネント取得
var body = gameObject.GetComponent<Rigidbody2D>();
//反射計算 var direction = new Vector2(speed, speed).normalized; body.velocity = direction * -10 * transform.localScale; // this.rd = GetComponent<Rigidbody2D>(); //this.speed = 100;
}
// Update is called once per frame
void Update()
{
/if (Input.GetMouseButton(0))//マウスの左で発射
{
this.startPos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
Vector2 endPos = Input.mousePosition;
Vector2 startDirection = -1 * (endPos - startPos).normalized;
this.Rigidbody2D.addForce(startDirection * speed);
}/
}
// public void Cllide()
// {
//if () {
//if(HP <= 0)
/{
Destroy(gameObject);
}///Destroy; }
//}
void TixedUpdate()
{
this.rd.velocity *= 0.9995f;
}
//当たり判定(IsTriggerにチェック入れる) /*if(c.name == "Obstacle" && HP > 0){HP -= 1;} //else if(c.name == "Player" && HP = 0) //{Destroy(gameObject); }*/ /* void FixedUpdate(){ this.Rigidbody.velocity *=0.995f;}*/ // void OnTriggerEnter2D(Collider2D c) /*if(c.name == "Obstacle(Clone)" && HP > 0){HP-=1;} else if(c.name == "Obstacle(Clone)" && HP = 0) {Destroy(gameObject); }*/
}
該当のソースコード
ブロック
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour
{
public int HP;
private Object Player;
private float ox;
private float oy;
private float x;
private float y;
// Start is called before the first frame update
void Start()
{
//物理演算コンポーネント取得
var body = gameObject.GetComponent<Rigidbody2D>();
//反射計算 var direction = new Vector2(0, 0).normalized; body.velocity = direction * -10 * transform.localScale; } // Update is called once per frame void Update() { // if(HP <= 0) // { // Destoroy(gameObject); // } } /*public void Destroy{ * if(ox)}*/ //当たり判定(IsTriggerにチェック入れる) void OnTriggerEnter2D(Collider2D c) { if (c.name == "Player1" || c.name == "Player2" || c.name == "Player3" || c.name == "Player4" || c.name == "Player5" || c.name == "Player6" || c.name == "Player7" || c.name == "Player8" || c.name == "Player9") { Destroy(gameObject); } } /*if(c.name == "Player1" || c.name == "Player2" || c.name == "Player3" || c.name == "Player4" || c.name == "Player5" || * c.name == "Player6" || c.name == "Player7" || c.name == "Player8" || c.name == "Player9" && HP > 0){-1;} else if(c.name == "Player1" || c.name == "Player2" || c.name == "Player3" || c.name == "Player4" || c.name == "Player5" || c.name == "Player6" || c.name == "Player7" || c.name == "Player8" || c.name == "Player9" && HP = 0) {Destroy(gameObject); }*/ //}
}
Unity2019.2.17.f1(64-bit)(C#)
コード
補足情報(FW/ツールのバージョンなど)
某下から複数のボールを飛ばすゲームをもとにしています。
あなたの回答
tips
プレビュー