Skip to content

Commit 65f3590

Browse files
committed
fix(Action): Throw by force
1 parent ff24f27 commit 65f3590

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

Assets/JCSUnity/Scripts/Actions/3D/JCS_3DThrowAction.cs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,49 @@ public class JCS_3DThrowAction : MonoBehaviour
5353
[Range(0.1f, 30.0f)]
5454
private float mGravityProduct = 1.0f;
5555

56+
[Tooltip("Type of the delta time.")]
57+
[SerializeField]
58+
private JCS_TimeType mTimeType = JCS_TimeType.DELTA_TIME;
59+
60+
[Tooltip("Rotate to look at forward location.")]
61+
[SerializeField]
62+
private bool mFaceFoward = false;
63+
64+
[Separator("By Force")]
65+
5666
[Tooltip("Force to hit the target.")]
5767
[SerializeField]
5868
[Range(0.1f, 300.0f)]
5969
private float mForce = 20.0f;
6070

61-
[Tooltip("Target time to hit the target.")]
71+
[Tooltip("Angle degree to hit the target.")]
6272
[SerializeField]
63-
[Range(1.0f, 10.0f)]
64-
private float mTime = 1.0f;
73+
private float mDegree = 0.0f;
6574

66-
[Tooltip("Type of the delta time.")]
67-
[SerializeField]
68-
private JCS_TimeType mTimeType = JCS_TimeType.DELTA_TIME;
75+
[Separator("By Time")]
6976

70-
[Tooltip("Rotate to look at forward location.")]
77+
[Tooltip("Target time to hit the target.")]
7178
[SerializeField]
72-
private bool mFaceFoward = false;
79+
[Range(1.0f, 10.0f)]
80+
private float mTime = 1.0f;
7381

7482
/* Setter & Getter */
7583

7684
public bool Active { get { return this.mActive; } set { this.mActive = value; } }
7785
public Vector3 Velocity { get { return this.mVelocity; } }
7886
public float GravityProduct { get { return this.mGravityProduct; } set { this.mGravityProduct = value; } }
79-
public float Force { get { return this.mForce; } set { this.mForce = value; } }
80-
public float Time { get { return this.mTime; } set { this.mTime = value; } }
8187
public JCS_TimeType DeltaTimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
8288
public bool FaceFoward { get { return this.mFaceFoward; } set { this.mFaceFoward = value; } }
8389

90+
public float Force { get { return this.mForce; } set { this.mForce = value; } }
91+
public float Degree { get { return this.mDegree; } set { this.mDegree = value; } }
92+
public float Time { get { return this.mTime; } set { this.mTime = value; } }
93+
94+
private float G
95+
{
96+
get { return JCS_Constants.GRAVITY * mGravityProduct; }
97+
}
98+
8499
/* Functions */
85100

86101
private void Update()
@@ -127,15 +142,6 @@ private void Test()
127142
/// <param name="startPos"> Point to start this action. </param>
128143
/// <param name="targetPos"> Point you want to hit. </param>
129144
/// <param name="time"> Certain time will reach the target position. </param>
130-
public void ThrowByTime(Vector3 startPos, Vector3 targetPos)
131-
{
132-
ThrowByTime(startPos, targetPos, mTime);
133-
}
134-
public void ThrowByTime(Vector3 startPos, Vector3 targetPos, float time)
135-
{
136-
this.transform.position = startPos;
137-
ThrowByTime(targetPos, time);
138-
}
139145
public void ThrowByTime(Vector3 targetPos)
140146
{
141147
ThrowByTime(targetPos, mTime);
@@ -147,7 +153,8 @@ public void ThrowByTime(Vector3 targetPos, float time)
147153
// Calculate initial velocity.
148154
mVelocity.x = displacement.x / time;
149155
mVelocity.z = displacement.z / time;
150-
mVelocity.y = (displacement.y - (JCS_Constants.GRAVITY * mGravityProduct * time * time / 2.0f)) / time;
156+
157+
mVelocity.y = (displacement.y - (0.5f * G * Mathf.Pow(time, 2))) / time;
151158

152159
// start the action.
153160
this.mActive = true;
@@ -159,23 +166,20 @@ public void ThrowByTime(Vector3 targetPos, float time)
159166
/// <param name="startPos"> Point to start this action. </param>
160167
/// <param name="targetPos"> Point you want to hit. </param>
161168
/// <param name="vel"> velocity to hit the point. </param>
162-
public void ThrowByForce(Vector3 startPos, Vector3 targetPos)
163-
{
164-
ThrowByForce(startPos, targetPos, mForce);
165-
}
166-
public void ThrowByForce(Vector3 startPos, Vector3 targetPos, float vel)
167-
{
168-
this.transform.position = startPos;
169-
ThrowByForce(targetPos, vel);
170-
}
171169
public void ThrowByForce(Vector3 targetPos)
172170
{
173-
ThrowByForce(targetPos, mForce);
171+
ThrowByForce(targetPos, mForce, mDegree);
174172
}
175-
public void ThrowByForce(Vector3 targetPos, float vel)
173+
public void ThrowByForce(Vector3 targetPos, float vel, float degree)
176174
{
177-
float distance = Vector3.Distance(targetPos, this.transform.position);
178-
float time = distance / vel;
175+
Vector3 displacement = targetPos - this.transform.position;
176+
var displacementXZ = new Vector3(displacement.x, 0, displacement.z);
177+
float horizontalDistance = displacementXZ.magnitude;
178+
179+
float angle = degree * Mathf.Deg2Rad;
180+
float horizontalVelocity = vel * Mathf.Cos(angle);
181+
182+
float time = horizontalDistance / horizontalVelocity;
179183

180184
ThrowByTime(targetPos, time);
181185
}
@@ -245,10 +249,16 @@ public static List<Vector3> GetArchByTime(
245249
public static List<Vector3> GetArchByForce(
246250
int pointCount,
247251
Vector3 startPos, Vector3 targetPos,
248-
float vel, float gravityProduct)
252+
float vel, float degree, float gravityProduct)
249253
{
250-
float distance = Vector3.Distance(targetPos, startPos);
251-
float time = distance / vel;
254+
Vector3 displacement = targetPos - startPos;
255+
var displacementXZ = new Vector3(displacement.x, 0, displacement.z);
256+
float horizontalDistance = displacementXZ.magnitude;
257+
258+
float angle = degree * Mathf.Deg2Rad;
259+
float horizontalVelocity = vel * Mathf.Cos(angle);
260+
261+
float time = horizontalDistance / horizontalVelocity;
252262

253263
return GetArchByTime(pointCount, startPos, targetPos, time, gravityProduct);
254264
}

docs/ScriptReference/Actions/3D/JCS_3DThrowAction.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ Throws a game object toward another game object.
88
|:----------------|:------------------------------------|
99
| mActive | Is this component active? |
1010
| mGravityProduct | Mulitply the gravity. |
11-
| mForce | Force to hit the target. |
12-
| mTime | Target time to hit the target. |
1311
| mTimeType | Type of the delta time. |
1412
| mFaceFoward | Rotate to look at forward location. |
15-
| GetArchByTime | Return a list of arch positions. |
16-
| GetArchByForce | Return a list of arch positions. |
13+
| mForce | Force to hit the target. |
14+
| mDegree | Angle degree to hit the target. |
15+
| mTime | Target time to hit the target. |
1716

1817
## Functions
1918

20-
| Name | Description |
21-
|:-------------|:------------------------------------------------|
22-
| ThrowByTime | Do the throw action by time. |
23-
| ThrowByForce | Do the throw action by calculate the kinematic. |
19+
| Name | Description |
20+
|:---------------|:------------------------------------------------|
21+
| ThrowByTime | Do the throw action by time. |
22+
| ThrowByForce | Do the throw action by calculate the kinematic. |
23+
| GetArchByTime | Return a list of arch positions. |
24+
| GetArchByForce | Return a list of arch positions. |

0 commit comments

Comments
 (0)