입사각과 반사각(총알의 튕김,당구)
2023. 2. 9. 11:57ㆍPublic/Math
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=falcon2026&logNo=221311522279
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotGunBulletPiece : Bullet
{
public override float Damage => 10f;
public override BulletType m_type => BulletType.Normal;
private Rigidbody2D rigid;
Vector2 lastVelocity;
Vector3 myDirection;
public void OnCollisionEnter2D(Collision2D coll)
{
BoxCollider2D bulletCollider = this.GetComponent<BoxCollider2D>();
if (coll.gameObject.tag == "Bullet")
bulletCollider.isTrigger = true;
else
bulletCollider.isTrigger = false;
//반사 사용
//Reflect(coll);
}
public void OnTriggerExit2D(Collider2D collision)
{
BoxCollider2D bulletCollider = this.GetComponent<BoxCollider2D>();
bulletCollider.isTrigger = false;
}
public override void Initialized(SpaceShip.PlayerNumber _number, int _index)
{
base.Initialized(_number, _index);
}
public void SetNormalizeDirection(Vector3 vec)
{
m_direction = vec;
}
private void UpdateDirectionRotate()
{
Vector3 dir = m_direction;
dir = m_playerNumber == SpaceShip.PlayerNumber.P2 ? dir *= -1f : m_direction;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
protected override void Move()
{
this.transform.position += m_direction * Time.deltaTime * m_speed;
}
private void FixedUpdate()
{
Move();
UpdateDirectionRotate();
}
//반사 사용시
private void Reflect(Collision2D coll)
{
if (coll.gameObject.tag == "OutLine")
{
Vector3 normalVec = coll.contacts[0].normal;
var dir = Vector3.Reflect(m_direction, normalVec);
m_direction = dir;
UpdateDirectionRotate();
}
}
}
'Public > Math' 카테고리의 다른 글
벡터를 각도로 변환: 아크탄젠트(2D추천) (0) | 2023.07.18 |
---|---|
사인,코사인 활용(탄막슈팅-산탄총) (0) | 2023.02.09 |
선형대수(Linear Algebra)(작성중) (수학으로 시작하는 3D 게임 개발) (0) | 2022.11.17 |
선형보간 공식 (0) | 2022.11.10 |
행렬 변환 (0) | 2022.11.09 |