Fixed bugs for pickable projectile; picking up if nearby

This commit is contained in:
Данил Ярош 2025-09-08 15:24:51 +03:00
parent 6ac8ba75d8
commit 3aa7e50a64
8 changed files with 101 additions and 29 deletions

View File

@ -9,6 +9,6 @@ PhysicsMaterial2D:
m_Name: Bouncy
serializedVersion: 2
friction: 0.4
bounciness: 0
bounciness: 0.7
m_FrictionCombine: 1
m_BounceCombine: 4

View File

@ -29,7 +29,7 @@ Transform:
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 2, y: 2, z: 2}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 8859797121973546175}
@ -110,7 +110,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5bf80b3f74c7fe04e8064ebca52b7ad4, type: 3}
m_Name:
m_EditorClassIdentifier:
initialSpeed: 15
initialSpeed: 20
maxLifetime: 5
alignRotationToVelocity: 1
knockback: 0
@ -129,7 +129,7 @@ MonoBehaviour:
attachPointChildName: HandPoint
sleepVelocity: 0.05
lockWhenLanded: 1
maxBounces: 3
maxBounces: 20
minBounceSpeed: 0.8
restitution: 0.6
tangentDamping: 0.85
@ -212,7 +212,7 @@ SpriteRenderer:
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_SortingOrder: 1
m_Sprite: {fileID: -2413806693520163455, guid: 7f76a84576351334b8517b4cd5666786, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0

View File

@ -2652,8 +2652,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
projectilePrefab: {fileID: -7990430535451479416, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
spawnIfMissing: 1
attachPoint: {fileID: 6550761233899429359}
spawnIfMissing: 0
attachPoint: {fileID: 6550761234898366363}
actionKey: 102
throwSpeed: 20
pickupRadius: 1.2

View File

@ -20342,6 +20342,63 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 4b0988a0068aee641a67f8aad32863f8, type: 3}
--- !u!1001 &5189102261111869253
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalPosition.x
value: 20.587563
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalPosition.y
value: 2
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1452460312376183192, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5993946430821065449, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
propertyPath: m_Name
value: PickableProjectile
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 7fd3a37dc702ece42b51e5867473f0ff, type: 3}
--- !u!1001 &8856198965438284572
PrefabInstance:
m_ObjectHideFlags: 0
@ -20413,3 +20470,4 @@ SceneRoots:
- {fileID: 8856198965438284572}
- {fileID: 1449142390}
- {fileID: 1443759818}
- {fileID: 5189102261111869253}

View File

@ -23,7 +23,7 @@ public class EnemyController : MonoBehaviour
if (playerObj == null)
{
playerObj = GameObject.FindGameObjectWithTag("Player");
Debug.Log("Player not found!");
//Debug.Log("Player not found!");
}
}

View File

@ -6,10 +6,11 @@ public class FireScript2D : MonoBehaviour
public BasicProjectile projectilePrefab;
public Transform muzzle;
public float shotSpeed = 20f;
public KeyCode actionKey = KeyCode.F;
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
if (Input.GetKeyDown(actionKey))
{
if (projectilePrefab == null) { Debug.LogWarning("No projectilePrefab"); return; }
@ -21,12 +22,12 @@ public class FireScript2D : MonoBehaviour
? (Vector2)muzzle.position
: (Vector2)transform.position + new Vector2(1.4f * facing, 0f);
BasicProjectile p = Instantiate(projectilePrefab, launchPos, Quaternion.identity);
p.transform.localScale = new Vector3(-facing * p.transform.localScale.x, p.transform.localScale.y, p.transform.localScale.z);
p.Launch(dir, shotSpeed);
BasicProjectile projectile = Instantiate(projectilePrefab, launchPos, Quaternion.identity);
projectile.transform.localScale = new Vector3(-facing * projectile.transform.localScale.x, projectile.transform.localScale.y, projectile.transform.localScale.z);
projectile.Launch(dir, shotSpeed);
var myCol = GetComponent<Collider2D>();
var pCol = p.GetComponent<Collider2D>();
var pCol = projectile.GetComponent<Collider2D>();
if (myCol && pCol) Physics2D.IgnoreCollision(myCol, pCol, true);
}
}

View File

@ -11,8 +11,8 @@ public class PickableProjectile : BasicProjectile
public State CurrentState { get; private set; } = State.Grounded;
[Header("Pickup/Attach")]
public Vector3 heldLocalOffset = new Vector3(0.25f, 0.1f, 0f);
public float ignoreOwnerTimeOnThrow = 0.15f;
public Vector3 heldLocalOffset = new(0.25f, 0.1f, 0f);
public float ignoreOwnerTimeOnThrow = 0.25f;
public string attachPointChildName = "HandPoint";
[Header("Physics on ground")]
@ -144,6 +144,13 @@ public class PickableProjectile : BasicProjectile
BecomeGrounded(collision);
}
//else if (CurrentState == State.Grounded)
//{
// if (collision.collider.CompareTag("Player"))
// {
// TryPickup(collision.gameObject);
// }
//}
}
protected override void OnImpactFinal(Collision2D collision)

View File

@ -1,4 +1,5 @@
using UnityEngine;
using Projectiles2D;
using UnityEngine;
public class PlayerFireScript : MonoBehaviour
{
@ -25,9 +26,23 @@ public class PlayerFireScript : MonoBehaviour
void Update()
{
var proj = P;
if (proj != null && proj.CurrentState != PickableProjectile.State.Held)
{
Vector3 grabFrom = attachPoint ? attachPoint.position : transform.position;
float dist = Vector2.Distance(proj.transform.position, grabFrom);
if (dist <= pickupRadius)
{
proj.TryPickup(gameObject, attachPoint);
return;
}
}
if (!Input.GetKeyDown(actionKey)) return;
var proj = P;
proj = P;
if (proj == null)
{
@ -48,28 +63,19 @@ public class PlayerFireScript : MonoBehaviour
float sx = transform.localScale.x;
float facing = Mathf.Sign(sx == 0 ? 1 : sx);
Vector2 dir = facing > 0 ? Vector2.right : Vector2.left;
Vector2 launchPos = (Vector2)transform.position + new Vector2(1.4f * facing, 0f);
proj.transform.localScale = new Vector3(-facing * proj.transform.localScale.x, proj.transform.localScale.y, proj.transform.localScale.z);
proj.transform.position = launchPos;
proj.Launch(dir, throwSpeed, gameObject);
return;
}
Vector3 grabFrom = attachPoint ? attachPoint.position : transform.position;
float dist = Vector2.Distance(proj.transform.position, grabFrom);
if (proj.CurrentState != PickableProjectile.State.Held && dist <= pickupRadius)
{
proj.TryPickup(gameObject, attachPoint);
return;
}
// Не в радиусе подбора — ничего не делаем
// Debug.Log("FireScript2D: projectile is too far to pick up.");
}
#if UNITY_EDITOR
void OnDrawGizmosSelected()
{
// визуально показать радиус подбора
Gizmos.color = new Color(0.2f, 0.8f, 1f, 0.35f);
Vector3 center = attachPoint ? attachPoint.position : transform.position;
Gizmos.DrawWireSphere(center, pickupRadius);