前提・実現したいこと
Playerというボールを操作し、敵のボールを場外に落としていくゲームを作っています。
とても大きな敵(同じくぼーる)を出現させplayerにむかって移動します。その時、playerに触れたとき、大きく吹き飛ばします。なお、powerupをplayerが所有していれば吹き飛ばす力が小さくなります。
Big Enemy
Nomal Enemy
Player
がここでは登場しますが、
Massは
Big Enemy 1500
Nomal Enemy 5
Player 5
です。
それぞれColliderのMaterialは
Dynamic Friction 0.6
Static Friction 0.6
Bounciness 1
Friction Combine Average
Bounce Combine Multiply
となっています。
発生している問題・エラーメッセージ
Big Enemyにplayerが接触したとき、吹き飛ばされるはずがくっついて離れなくなってしまうことがあります。
Nomal Enemyも同じように吹き飛ばされるスクリプトを書いてありますが、Nomal Enemyにはこのようなことがありません。
また特に、Big Enemyが登場するとき、上空から落下してくるのですが、落下しGroundに着地した瞬間にPlayerがBig Enemyに触れているとこの問題が起きやすいように感じます。さらに、BigEnemyが出現した瞬間に、それの下に潜り込むように接触しに行くと、それ以降PlayerがBigEnemyから離れなくなりました。
######予想
敵の下に潜り込んだ時に起こっているため、BigEnemyから下向きの力が加わって、弾き飛ばされるはずが、地面に押し付けられているようになっているのかもしれません。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BigEnemy : MonoBehaviour 6{ 7 private GameObject player; 8 private GameObject mainCamera; 9 10 private Rigidbody bigEnemyRb; 11 12 public float bigEnemySpeed = 10.0f; 13 public float blowOffStrengh = 15.0f; 14 15 private bool onGround = false; 16 17 private PlayerController playerControllerScript; 18 19 // Start is called before the first frame update 20 void Start() 21 { 22 player = GameObject.Find("Player"); 23 mainCamera = GameObject.Find("Main Camera"); 24 bigEnemyRb = GetComponent<Rigidbody>(); 25 playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>(); 26 27 } 28 29 // Update is called once per frame 30 void Update() 31 { 32 if (onGround == true) 33 { 34 moveToPlayer(); 35 } 36 } 37 38 private void moveToPlayer() 39 { 40 if (!playerControllerScript.gameOver) 41 { 42 if (playerControllerScript.readyToRanchSpecialWeapon) 43 { 44 bigEnemyRb.velocity = Vector3.zero; 45 } 46 else 47 { 48 Vector3 lookDirection = (player.transform.position - transform.position).normalized; 49 50 bigEnemyRb.AddForce(lookDirection * bigEnemySpeed); 51 } 52 } 53 } 54 private void OnCollisionEnter(Collision collision) 55 { 56 if (collision.gameObject.CompareTag("Nomal Enemy")) 57 { 58 Rigidbody nomalEnemyRigidbody = collision.gameObject.GetComponent<Rigidbody>(); 59 Vector3 awayFromBigEnemy = collision.gameObject.transform.position - transform.position; 60 61 nomalEnemyRigidbody.AddForce(awayFromBigEnemy * blowOffStrengh, ForceMode.Impulse); 62 } 63 64 if (collision.gameObject.CompareTag("Player")) 65 { 66 Rigidbody nomalEnemyRigidbody = collision.gameObject.GetComponent<Rigidbody>(); 67 Vector3 awayFromBigEnemy = collision.gameObject.transform.position - transform.position; 68 69 if (playerControllerScript.hasPowerup) 70 { 71 blowOffStrengh = 10f; 72 } 73 else 74 { 75 blowOffStrengh = 30f; 76 } 77 nomalEnemyRigidbody.AddForce(awayFromBigEnemy * blowOffStrengh, ForceMode.Impulse); 78 } 79 80 if (collision.gameObject.CompareTag("Ground")) 81 { 82 onGround = true; 83 StartCoroutine(ShakeCamera()); 84 } 85 } 86 IEnumerator ShakeCamera() 87 { 88 float shakeY = 10f; 89 float shakeInterval = 0.1f; 90 for (int i = 0; i < 15; i++) 91 { 92 yield return new WaitForSeconds(shakeInterval); 93 94 mainCamera.transform.Translate(0, shakeY, 0); 95 96 if (i % 2 == 0) 97 { 98 shakeY = shakeY * 0.5f * -1; 99 100 } 101 102 shakeInterval -= 0.01f; 103 } 104 105 mainCamera.transform.position = new Vector3(0, 10, -20); 106 } 107 108} 109
######補足 Playerのscript
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5using UnityEngine.UI; 6 7public class PlayerController : MonoBehaviour 8{ 9 private GameObject focalPoint; 10 public GameObject powerupIndicator; 11 public GameObject specialWeaponIndicator; 12 public GameObject nomalEnemyPrefab; 13 14 private Rigidbody playerRb; 15 private Rigidbody nomalEnemyPrefabRb; 16 17 public float powerUpStrengh = 15.0f; 18 public float defaltSpeed = 5.0f; 19 20 public bool hasPowerup = false; 21 public bool readyToRanchSpecialWeapon = false; 22 public bool hasSpecialWeapon = false; 23 public bool gameOver = false; 24 public static bool cameraForwardType = false; 25 public bool onGround; 26 public bool blownAwayByPlayerWithSpecialWeapon = false; 27 28 29 public ParticleSystem bigShockParticle; 30 public ParticleSystem smallShockParticle; 31 32 private AudioSource playerAudio; 33 public AudioClip highBounceSound; 34 public AudioClip coinSound; 35 public AudioClip lowBounceSound; 36 public AudioClip BigBounceSound; 37 public AudioClip getSpecialWeaponSound; 38 39 // Start is called before the first frame update 40 void Start() 41 { 42 Time.timeScale = 1.0f; 43 44 focalPoint = GameObject.Find("Focal Point"); 45 46 playerRb = GetComponent<Rigidbody>(); 47 nomalEnemyPrefabRb = nomalEnemyPrefab.GetComponent<Rigidbody>(); 48 49 playerAudio = GetComponent<AudioSource>(); 50 } 51 52 // Update is called once per frame 53 void Update() 54 { 55 Move(); 56 57 Jump(); 58 59 Destroy(); 60 } 61 62 private void Move() 63 { 64 if (!readyToRanchSpecialWeapon) 65 { 66 float speed; 67 if (cameraForwardType) 68 { 69 float forwardInput = Input.GetAxis("Vertical"); 70 71 if (onGround) 72 { 73 speed = defaltSpeed; 74 } 75 else 76 { 77 speed = defaltSpeed * 0.3f; 78 } 79 80 playerRb.AddForce(focalPoint.transform.forward * speed * forwardInput); 81 } 82 else 83 { 84 float verticalInput = Input.GetAxis("Vertical"); 85 float horizontalInput = Input.GetAxis("Horizontal"); 86 87 if (onGround) 88 { 89 speed = defaltSpeed; 90 } 91 else 92 { 93 speed = defaltSpeed * 0.3f; 94 } 95 playerRb.AddForce(focalPoint.transform.forward * speed * verticalInput); 96 playerRb.AddForce(focalPoint.transform.right * speed * horizontalInput); 97 } 98 } 99 } 100 101 102 103 104 private void OnTriggerEnter(Collider other) 105 { 106 if (other.CompareTag("Powerup") && !hasPowerup) 107 { 108 hasPowerup = true; 109 Destroy(other.gameObject); 110 StartCoroutine(PowerupCountdownRoutine()); 111 StartCoroutine(SetPowerupIndicatorPosition()); 112 113 playerAudio.PlayOneShot(coinSound, 0.4f); 114 } 115 116 if (other.CompareTag("Special Weapon") && !hasSpecialWeapon && onGround) 117 { 118 119 hasSpecialWeapon = true; 120 Destroy(other.gameObject); 121 122 StartCoroutine(SetSpecialWeaponIndicatorPosition()); 123 124 StartCoroutine(SetSpecialWeapon()); 125 126 playerAudio.PlayOneShot(getSpecialWeaponSound, 1f); 127 } 128 } 129 130 IEnumerator PowerupCountdownRoutine() 131 { 132 powerupIndicator.gameObject.SetActive(true); 133 yield return new WaitForSeconds(7); 134 hasPowerup = false; 135 powerupIndicator.gameObject.SetActive(false); 136 } 137 138 IEnumerator SetPowerupIndicatorPosition() 139 { 140 while (hasPowerup) 141 { 142 powerupIndicator.transform.position = transform.position + new Vector3(0, -0.5f, 0); 143 144 yield return null; 145 } 146 } 147 148 149 150 private void OnCollisionEnter(Collision collision) 151 { 152 if ((collision.gameObject.CompareTag("Nomal Enemy") || collision.gameObject.CompareTag("Mitosis Enemy")) && hasPowerup) 153 { 154 Rigidbody nomalEnemyRigidbody = collision.gameObject.GetComponent<Rigidbody>(); 155 Vector3 awayFromPlayer = collision.gameObject.transform.position - transform.position; 156 157 nomalEnemyRigidbody.AddForce(awayFromPlayer * powerUpStrengh, ForceMode.Impulse); 158 smallShockParticle.Play(); 159 //Debug.Log("Player collided with: " + collision.gameObject.name + " with powerup set to " + hasPowerup); 160 161 playerAudio.PlayOneShot(highBounceSound, 4f); 162 } 163 else if ((collision.gameObject.CompareTag("Nomal Enemy") || collision.gameObject.CompareTag("Mitosis Enemy")) && !hasPowerup) 164 { 165 playerAudio.PlayOneShot(lowBounceSound, 0.6f); 166 } 167 168 169 } 170 171}
補足情報(FW/ツールのバージョンなど)
unity 2020.1.2f1
windows10
回答1件
あなたの回答
tips
プレビュー