Unity2D ボールを壁で跳ね返す
解決済
回答 2
投稿
- 評価
- クリップ 0
- VIEW 655

退会済みユーザー
前提・実現したいこと
ボールを壁で跳ね返したい
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseClickPoint : MonoBehaviour
{
public static Vector2 mousePosition;
private Mesh mesh;
public Material material;
private bool meshDrawed;
public static Vector2 bMousePos = new Vector2(1000,0);
public static bool goAllowedBall;
void Start()
{
mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0,0,0),
new Vector3(0.25f,0,0),
new Vector3(0,0.25f,0),
new Vector3(0.25f,0.25f,0),
};
mesh.uv = new Vector2[]
{
new Vector2(0,0),
new Vector2(0,1),
new Vector2(1,0),
new Vector2(1,1),
};
mesh.triangles = new int[]
{
0,1,2,
1,3,2,
};
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
void Update()
{
if (Game.gameStarted)
{
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x > 2.4f)
{
mousePosition = new Vector2(2.28f, pos.y);
}
if (pos.x < -2.5f)
{
mousePosition = new Vector2(-2.5f, pos.y);
}
if (pos.x < 2.4f && pos.x > -2.5f)
{
if (Input.GetMouseButtonDown(0))
{
meshDrawed = true;
bMousePos = new Vector2(mousePosition.x, mousePosition.y);
goAllowedBall = true;
}
mousePosition = new Vector2(pos.x, pos.y);
}
}
if (meshDrawed)
{
Graphics.DrawMesh(mesh, bMousePos, Quaternion.identity, material, 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BallMove : MonoBehaviour
{
//速度
public Vector2 velocity = new Vector2(0.1f, 0f);
private Vector2 ballPosition;
private float fVelocity = 0.1f;
private float fAngle;
public GameObject Ball;
//二点間の角度を求める
float ExposeDegree(Vector2 basePos,Vector2 targetPos)
{
//基準の位置から目標の位置までのベクトルを求める
Vector2 dif = targetPos - basePos;
//ラジアン
float radian = Mathf.Atan2(dif.y, dif.x);
//角度
float degree = radian * Mathf.Rad2Deg;
return degree;
}
float ExposeAngle(float num)
{
float angle = (num * 3.14f) / 180;
return angle;
}
private void Start()
{
ballPosition = Ball.transform.position;
}
private void Update()
{
if (MouseClickPoint.goAllowedBall)
{
CalculateVelocity();
ballPosition += velocity;
ReflectToWall();
Ball.transform.position = ballPosition;
}
}
private void CalculateVelocity()
{
Vector2 firstPosition = new Vector2(0, 0);
Vector2 targetPosition = MouseClickPoint.bMousePos;
float degree = ExposeDegree(firstPosition, targetPosition);
fAngle = ExposeAngle(degree);
velocity.x = fVelocity * Mathf.Cos(fAngle);
velocity.y = fVelocity * Mathf.Sin(fAngle);
}
private void ReflectToWall()
{
if(ballPosition.x > 2.4f)
{
ballPosition.x = 2.4f;
velocity.x = -velocity.x;
}
}
}
試したこと
ReflectToWall()でボールが壁で跳ね返るようにしたかったのですが、どうやってもY方向に直線に進むだけです。どのサイトを見ても、速度をマイナスに反転させれば壁で跳ね返ると書いてあるのにまったく成功しません。
基本的なことなのに出来ず、完全にお手上げです。どうしたら壁でボールを反射させられますか?
回答お願いします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
velocityで速度を持っていて、そこを反転するまではあっていますが、
CalculateVelocityでvelocityを上書きしているように見えます
これだとせっかく符号を反転しても意味がないんじゃないでしょうか
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
これで壁で反射します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BallMove : MonoBehaviour
{
//速度
public Vector2 velocity = new Vector2(0.1f, 0f);
private Vector2 ballPosition;
private float fVelocity = 0.25f;
private float fAngle;
public GameObject Ball;
private bool goStoped;
//二点間の角度を求める
float ExposeDegree(Vector2 basePos,Vector2 targetPos)
{
//基準の位置から目標の位置までのベクトルを求める
Vector2 dif = targetPos - basePos;
//ラジアン
float radian = Mathf.Atan2(dif.y, dif.x);
//角度
float degree = radian * Mathf.Rad2Deg;
return degree;
}
float ExposeAngle(float num)
{
float angle = (num * 3.14f) / 180;
return angle;
}
private void Start()
{
ballPosition = Ball.transform.position;
}
private void Update()
{
if (MouseClickPoint.goAllowedBall)
{
if(goStoped == false)
{
CalculateVelocity();
}
ballPosition += velocity;
Ball.transform.position = ballPosition;
}
if (ballPosition.x > 2.4f)
{
goStoped = true;
ReflectToWall_Right();
}
if(ballPosition.x < -2.4f)
{
goStoped = true;
ReflectToWall_Left();
}
}
private void CalculateVelocity()
{
Vector2 firstPosition = new Vector2(0, 0);
Vector2 targetPosition = MouseClickPoint.bMousePos;
float degree = ExposeDegree(firstPosition, targetPosition);
fAngle = ExposeAngle(degree);
velocity.x = fVelocity * Mathf.Cos(fAngle);
velocity.y = fVelocity * Mathf.Sin(fAngle);
}
private void ReflectToWall_Right()
{
ballPosition.x = 2.4f;
velocity.x = -velocity.x;
}
private void ReflectToWall_Left()
{
ballPosition.x = -2.4f;
velocity.x = -velocity.x;
}
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.11%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2020/03/18 18:13