Rework hitscaning on cannon fire

This commit is contained in:
Soulaha Balde 2022-05-15 10:05:34 -04:00
parent ba99bbb4f0
commit e6fa39fb67
4 changed files with 777 additions and 311 deletions

View File

@ -454,9 +454,9 @@ RectTransform:
m_Father: {fileID: 8183383574400522882}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 100.00001, y: -25}
m_SizeDelta: {x: 200, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &3947095809116700151
@ -1114,7 +1114,7 @@ RectTransform:
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 10, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -1189,8 +1189,8 @@ RectTransform:
m_Father: {fileID: 7617209483920801355}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMin: {x: 0.775, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -1265,9 +1265,9 @@ RectTransform:
m_Father: {fileID: 6054892815323966725}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 100.00001, y: -25}
m_SizeDelta: {x: 200, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5251019473499131800
@ -4023,8 +4023,8 @@ RectTransform:
m_Father: {fileID: 7832311567031281615}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMin: {x: 0.775, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -4234,7 +4234,7 @@ RectTransform:
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 10, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -4310,9 +4310,9 @@ RectTransform:
m_Father: {fileID: 4514125314489927979}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 200, y: -104.26691}
m_SizeDelta: {x: 441.22693, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &5662559955727251511
@ -4652,9 +4652,9 @@ RectTransform:
m_Father: {fileID: 4514125314489927979}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 200, y: -34.755634}
m_SizeDelta: {x: 441.22693, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2360335056540398615
@ -4831,9 +4831,9 @@ RectTransform:
m_Father: {fileID: 8477939615244931290}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 320.61346, y: -25}
m_SizeDelta: {x: 241.2269, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &5381892342036000582
@ -5332,8 +5332,8 @@ RectTransform:
m_Father: {fileID: 5471430262505720550}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMin: {x: 0.775, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -5410,9 +5410,9 @@ RectTransform:
m_Father: {fileID: 8183383574400522882}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 320.61346, y: -25}
m_SizeDelta: {x: 241.2269, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2623087485492258608
@ -5815,9 +5815,9 @@ RectTransform:
m_Father: {fileID: 6054892815323966725}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 320.61346, y: -25}
m_SizeDelta: {x: 241.2269, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &7282530242272405843
@ -5970,9 +5970,9 @@ RectTransform:
m_Father: {fileID: 4514125314489927979}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 200, y: -173.77817}
m_SizeDelta: {x: 441.22693, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &6358657304481751404
@ -7577,9 +7577,9 @@ RectTransform:
m_Father: {fileID: 8477939615244931290}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 100.00001, y: -25}
m_SizeDelta: {x: 200, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &349148022202180518
@ -7941,7 +7941,7 @@ RectTransform:
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMax: {x: 0.775, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 10, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
@ -8133,7 +8133,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &6873527208435646568
RectTransform:
m_ObjectHideFlags: 0

View File

@ -218,7 +218,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
cannon: {fileID: 6969840675257017922}
projectile: {fileID: 5630905120393344806, guid: 70e77cdd333989d4193d3d85029d8cbe, type: 3}
lookDepth: 400
lookDepth: 150
cannonForce: 500
fireRate: 0.5
fireTimer: 0

File diff suppressed because it is too large Load Diff

View File

@ -35,20 +35,6 @@ public class CannonScript : MonoBehaviour
if(fireTimer >= fireRate){
Fire();
fireTimer = 0;
RaycastHit hit;
if (Physics.Raycast(transform.position, lookDir, out hit, Mathf.Infinity))
{
Debug.DrawRay(transform.position, lookDir * hit.distance, Color.yellow);
GameObject hitObject = hit.collider.gameObject;
if (hitObject.tag == "Enemy")
{
hitObject.GetComponent<Enemy>().IsShot();
}
}
}
}else if(fireTimer < fireRate){
fireTimer += Time.deltaTime;
@ -59,6 +45,18 @@ public class CannonScript : MonoBehaviour
GameObject proj = Instantiate(projectile, cannon.transform.position, cannon.transform.rotation);
proj.GetComponent<Projectile>().SetDamage(damage);
proj.GetComponent<Rigidbody>().AddForce(cannonForce * lookDir, ForceMode.Impulse);
RaycastHit hit;
if (Physics.Raycast(transform.position, lookDir, out hit, Mathf.Infinity))
{
Debug.DrawRay(transform.position, lookDir * hit.distance, Color.yellow, 0.5f);
GameObject hitObject = hit.collider.gameObject;
if (hitObject.CompareTag("Enemy"))
{
hitObject.GetComponent<Enemy>().IsShot();
}
}
if(bullets > 1){
//Pick random offset from lookDir
for (int i = 0; i < bullets-1; i++)
@ -66,14 +64,39 @@ public class CannonScript : MonoBehaviour
proj = Instantiate(projectile, cannon.transform.position, cannon.transform.rotation);
proj.GetComponent<Projectile>().SetDamage(damage);
proj.GetComponent<Rigidbody>().AddForce(cannonForce * (lookDir+offsets[i]), ForceMode.Impulse);
if (Physics.Raycast(transform.position, (lookDir+offsets[i]), out hit, Mathf.Infinity))
{
Debug.DrawRay(transform.position, (lookDir+offsets[i]) * hit.distance, Color.yellow, 0.5f);
GameObject hitObject = hit.collider.gameObject;
if (hitObject.CompareTag("Enemy"))
{
hitObject.GetComponent<Enemy>().IsShot();
}
}
}
}
}
private Vector3 GetMouseWorldPosition(){
Vector3 screenPos = Mouse.current.position.ReadValue();
screenPos.z = lookDepth;
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPos);
Ray ray = Camera.main.ScreenPointToRay(screenPos);
RaycastHit hit;
Vector3 worldPoint = Vector3.zero;
if (Physics.Raycast(ray, out hit)) {
if(hit.transform.gameObject.CompareTag("Enemy")){
worldPoint = hit.point;
}else{
screenPos.z = lookDepth;
worldPoint = Camera.main.ScreenToWorldPoint(screenPos);
}
}
if(worldPoint == Vector3.zero){
screenPos.z = lookDepth;
worldPoint = Camera.main.ScreenToWorldPoint(screenPos);
}
return worldPoint;
}