前提・実現したいこと
rigitbodyを持ったオブジェクト同士がぶつかった時の反動をなくしたい
発生している問題
rigitbodyを持ったオブジェクト同士が ぶつかった時に反動でそれぞれが勝手に動いてしまうので 反動をなくしてピタッと止まるようにしたい
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class boxRscript : MonoBehaviour 7{ 8 private Rigidbody2D rb; 9 public float speed = 1; 10 public GameObject b; 11 private bool moving = false; 12 public int goalPoint; 13 // Start is called before the first frame update 14 void Start() 15 { 16 rb = GetComponent<Rigidbody2D>(); 17 } 18 19 // Update is called once per frame 20 void FixedUpdate() 21 { 22 if (moving == false) 23 { 24 if (Input.GetKeyDown(KeyCode.UpArrow)) 25 { 26 rb.velocity = new Vector2(rb.velocity.x, speed); 27 rb.constraints = RigidbodyConstraints2D.FreezePositionX; 28 moving = true; 29 } 30 31 if (Input.GetKeyDown(KeyCode.RightArrow)) 32 { 33 rb.velocity = new Vector2(speed, rb.velocity.y); 34 rb.constraints = RigidbodyConstraints2D.FreezePositionY; 35 moving = true; 36 } 37 38 if (Input.GetKeyDown(KeyCode.DownArrow)) 39 { 40 rb.velocity = new Vector2(rb.velocity.x, -speed); 41 rb.constraints = RigidbodyConstraints2D.FreezePositionX; 42 moving = true; 43 } 44 45 if (Input.GetKeyDown(KeyCode.LeftArrow)) 46 { 47 rb.velocity = new Vector2(-speed, rb.velocity.y); 48 rb.constraints = RigidbodyConstraints2D.FreezePositionY; 49 moving = true; 50 } 51 52 if (Input.GetKeyDown(KeyCode.W)) 53 { 54 b.GetComponent<Rigidbody2D>().velocity = new Vector2(b.GetComponent<Rigidbody2D>().velocity.x, speed); 55 b.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX; 56 moving = true; 57 } 58 59 if (Input.GetKeyDown(KeyCode.D)) 60 { 61 b.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, b.GetComponent<Rigidbody2D>().velocity.y); 62 b.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionY; 63 moving = true; 64 } 65 66 if (Input.GetKeyDown(KeyCode.S)) 67 { 68 b.GetComponent<Rigidbody2D>().velocity = new Vector2(b.GetComponent<Rigidbody2D>().velocity.x, -speed); 69 b.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX; 70 moving = true; 71 } 72 73 if (Input.GetKeyDown(KeyCode.A)) 74 { 75 b.GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, b.GetComponent<Rigidbody2D>().velocity.y); 76 b.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionY; 77 moving = true; 78 } 79 } 80 81 if(rb.velocity.x == 0 && rb.velocity.y == 0 && b.GetComponent<Rigidbody2D>().velocity.x == 0 && b.GetComponent<Rigidbody2D>().velocity.y == 0) 82 { 83 moving = false; 84 } 85 86 if(rb.velocity.x > 0 && rb.velocity.y > 0) 87 { 88 b.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition; 89 } 90 91 if(b.GetComponent<Rigidbody2D>().velocity.x > 0 && b.GetComponent<Rigidbody2D>().velocity.y > 0) 92 { 93 rb.constraints = RigidbodyConstraints2D.FreezePosition; 94 } 95 96 if (goalPoint == 2) 97 { 98 SceneManager.LoadScene("clearScene 1"); 99 } 100 101 if(Input.GetKeyDown(KeyCode.Space)) 102 { 103 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 104 SceneManager.LoadScene(sceneIndex); 105 } 106 } 107 108 private void OnTriggerEnter2D(Collider2D collision) 109 { 110 if (collision.gameObject.name == "goalR") 111 { 112 goalPoint += 1; 113 } 114 } 115 116 private void OnTriggerExit2D(Collider2D other) 117 { 118 if (other.gameObject.name == "goalR") 119 { 120 goalPoint -= 1; 121 } 122 } 123} 124
試したこと
FreezePositionをうまく使おうとしましたが、難しかった
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。