質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

2040閲覧

Unity2D ボールを壁で跳ね返す

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/03/17 13:40

前提・実現したいこと

ボールを壁で跳ね返したい

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MouseClickPoint : MonoBehaviour 6{ 7 public static Vector2 mousePosition; 8 private Mesh mesh; 9 public Material material; 10 private bool meshDrawed; 11 public static Vector2 bMousePos = new Vector2(1000,0); 12 public static bool goAllowedBall; 13 void Start() 14 { 15 mesh = new Mesh(); 16 mesh.vertices = new Vector3[] 17 { 18 new Vector3(0,0,0), 19 new Vector3(0.25f,0,0), 20 new Vector3(0,0.25f,0), 21 new Vector3(0.25f,0.25f,0), 22 }; 23 24 mesh.uv = new Vector2[] 25 { 26 new Vector2(0,0), 27 new Vector2(0,1), 28 new Vector2(1,0), 29 new Vector2(1,1), 30 }; 31 mesh.triangles = new int[] 32 { 33 0,1,2, 34 1,3,2, 35 }; 36 mesh.RecalculateNormals(); 37 mesh.RecalculateBounds(); 38 } 39 40 void Update() 41 { 42 if (Game.gameStarted) 43 { 44 var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 45 if (pos.x > 2.4f) 46 { 47 mousePosition = new Vector2(2.28f, pos.y); 48 } 49 if (pos.x < -2.5f) 50 { 51 mousePosition = new Vector2(-2.5f, pos.y); 52 } 53 if (pos.x < 2.4f && pos.x > -2.5f) 54 { 55 if (Input.GetMouseButtonDown(0)) 56 { 57 meshDrawed = true; 58 bMousePos = new Vector2(mousePosition.x, mousePosition.y); 59 goAllowedBall = true; 60 } 61 mousePosition = new Vector2(pos.x, pos.y); 62 } 63 } 64 if (meshDrawed) 65 { 66 Graphics.DrawMesh(mesh, bMousePos, Quaternion.identity, material, 0); 67 } 68 } 69} 70

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5public class BallMove : MonoBehaviour 6{ 7 //速度 8 public Vector2 velocity = new Vector2(0.1f, 0f); 9 private Vector2 ballPosition; 10 private float fVelocity = 0.1f; 11 private float fAngle; 12 public GameObject Ball; 13 //二点間の角度を求める 14 float ExposeDegree(Vector2 basePos,Vector2 targetPos) 15 { 16 //基準の位置から目標の位置までのベクトルを求める 17 Vector2 dif = targetPos - basePos; 18 //ラジアン 19 float radian = Mathf.Atan2(dif.y, dif.x); 20 //角度 21 float degree = radian * Mathf.Rad2Deg; 22 return degree; 23 } 24 float ExposeAngle(float num) 25 { 26 float angle = (num * 3.14f) / 180; 27 return angle; 28 } 29 private void Start() 30 { 31 ballPosition = Ball.transform.position; 32 } 33 private void Update() 34 { 35 if (MouseClickPoint.goAllowedBall) 36 { 37 CalculateVelocity(); 38 ballPosition += velocity; 39 ReflectToWall(); 40 Ball.transform.position = ballPosition; 41 } 42 } 43 private void CalculateVelocity() 44 { 45 Vector2 firstPosition = new Vector2(0, 0); 46 Vector2 targetPosition = MouseClickPoint.bMousePos; 47 float degree = ExposeDegree(firstPosition, targetPosition); 48 fAngle = ExposeAngle(degree); 49 velocity.x = fVelocity * Mathf.Cos(fAngle); 50 velocity.y = fVelocity * Mathf.Sin(fAngle); 51 } 52 private void ReflectToWall() 53 { 54 if(ballPosition.x > 2.4f) 55 { 56 ballPosition.x = 2.4f; 57 velocity.x = -velocity.x; 58 } 59 } 60} 61

試したこと

ReflectToWall()でボールが壁で跳ね返るようにしたかったのですが、どうやってもY方向に直線に進むだけです。どのサイトを見ても、速度をマイナスに反転させれば壁で跳ね返ると書いてあるのにまったく成功しません。
基本的なことなのに出来ず、完全にお手上げです。どうしたら壁でボールを反射させられますか?
回答お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

これで壁で反射します。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5public class BallMove : MonoBehaviour 6{ 7 //速度 8 public Vector2 velocity = new Vector2(0.1f, 0f); 9 private Vector2 ballPosition; 10 private float fVelocity = 0.25f; 11 private float fAngle; 12 public GameObject Ball; 13 private bool goStoped; 14 //二点間の角度を求める 15 float ExposeDegree(Vector2 basePos,Vector2 targetPos) 16 { 17 //基準の位置から目標の位置までのベクトルを求める 18 Vector2 dif = targetPos - basePos; 19 //ラジアン 20 float radian = Mathf.Atan2(dif.y, dif.x); 21 //角度 22 float degree = radian * Mathf.Rad2Deg; 23 return degree; 24 } 25 float ExposeAngle(float num) 26 { 27 float angle = (num * 3.14f) / 180; 28 return angle; 29 } 30 private void Start() 31 { 32 ballPosition = Ball.transform.position; 33 } 34 private void Update() 35 { 36 if (MouseClickPoint.goAllowedBall) 37 { 38 if(goStoped == false) 39 { 40 CalculateVelocity(); 41 } 42 ballPosition += velocity; 43 Ball.transform.position = ballPosition; 44 } 45 if (ballPosition.x > 2.4f) 46 { 47 goStoped = true; 48 ReflectToWall_Right(); 49 } 50 if(ballPosition.x < -2.4f) 51 { 52 goStoped = true; 53 ReflectToWall_Left(); 54 } 55 } 56 private void CalculateVelocity() 57 { 58 Vector2 firstPosition = new Vector2(0, 0); 59 Vector2 targetPosition = MouseClickPoint.bMousePos; 60 float degree = ExposeDegree(firstPosition, targetPosition); 61 fAngle = ExposeAngle(degree); 62 velocity.x = fVelocity * Mathf.Cos(fAngle); 63 velocity.y = fVelocity * Mathf.Sin(fAngle); 64 } 65 private void ReflectToWall_Right() 66 { 67 ballPosition.x = 2.4f; 68 velocity.x = -velocity.x; 69 } 70 private void ReflectToWall_Left() 71 { 72 ballPosition.x = -2.4f; 73 velocity.x = -velocity.x; 74 } 75} 76

投稿2020/03/18 09:13

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

velocityで速度を持っていて、そこを反転するまではあっていますが、
CalculateVelocityでvelocityを上書きしているように見えます
これだとせっかく符号を反転しても意味がないんじゃないでしょうか

投稿2020/03/17 23:51

izmktr

総合スコア2856

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/03/18 09:13

出来ました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問