- Home /
Throw a weapon in a physics arc with a lead to a target?
I am building a side-scrolling game and trying to throw grenades at characters on the screen at the same time, with the point of origin being the camera. While I've gotten the arc to work, it still needs a lead time in order to actually be visible on the screen. Can anyone suggest a method of doing a light arc with a lead to hit a target, using either physics or transform? I'm willing to switch if there's a better method.
question, is your "hero" (whatever throws the grenade) and your "enemy" always moving at the same speed in your game?
secondly TBC this is a 2D game right? when you say it's thrown "from the camera" that sounds a bit strange - do you mean from the plane of action, basically from the middle of what will be the screen?
for the record it would be a parabola I guess. "arc" often means a segment of a circle.
No, the target can vary it's speed up until the first time it's hit with an explosive radius. However, the camera is following at roughly the same speed - the target varies just enough to be slightly harder to hit. Yes, this is a 2D game, and the weapon is being thrown from essentially the middle of the screen to make it look like the player is throwing it. Once the target is in the air it's speed depends on the blast power. I guess parabola is also correct ^^;
Answer by Montraydavis · Oct 29, 2012 at 12:19 AM
This is sort of an answer since it gets the job done, but, I too am wanting to know more about this topic. I have some basic scripting that instantiates, levels at a height of x, and shoots toward the enemy until hit, then explodes and dies.
//Shuriken.js
var Target : GameObject ;
var TargetDistance : float ;
var ShurikenSpeed : float = 50 ;
var Life : float = 3 ;
function Start () {
Target = GameObject.Find ( "Troll" ) ;// Assign Target To ZTarget
}
function Update () {
Life -= 1 * Time.deltaTime ;
if ( Mathf.Ceil ( Life ) == 0 )
{
Instantiate( Resources.Load ( "Detonator-Crazysparks" ), transform.position, Quaternion.identity ) ;
GameObject.Destroy ( gameObject ) ;
}
if ( Mathf.Ceil ( TargetDistance ) == 2 ){
ShurikenSpeed = 10 ;
}
transform.rotation.y = 90 ;s
transform.position += transform.forward * ShurikenSpeed * Time.deltaTime; // Move Entity Toward Target
//transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x , Target.transform.position.y+0.1, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
transform.LookAt ( Target.transform ) ;
transform.position.y = 1;
}
function OnTriggerEnter ( collision : Collider )
{
if ( collision.tag == "Zombie" ){
Instantiate( Resources.Load ( "Detonator-Crazysparks" ), transform.position, Quaternion.identity ) ;
GameObject.Destroy ( gameObject ) ;
}
}
//NinjaController
function Update ( ) {
if ( Input.GetKeyDown ( KeyCode.S ) && animator.GetFloat ( "CoolDown" ) < 0.1 )
{
Instantiate( Resources.Load ( "Shuriken" ), Vector3 ( transform.position.x, 1, transform.position.z), Quaternion.identity) ;
}
}
Anyone have a better method than this, would be greatly appreciated to post ! :D
I think I may go with Transform ins$$anonymous$$d of Physics-based. Hoping it'll help keep the weapons on-screen as things move after they're fired.
The script above has no physics involvement, and is based solely off of transform. Thumbs up if this has helped.
No luck.. I tried it and still didn't get the thing working. I'm using the code from this as my base, but I need to add a lead time to the projectile when it's in the arc so it doesn't go off screen: http://answers.unity3d.com/questions/145972/how-to-make-enemy-canon-ball-fall-on-mooving-targe.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613083022im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Calculate a trajectory in all possible directions (2D). 2 Answers
How do I arc a rigidbody so it lands on a target? 2 Answers
Spear throwing with HTC Vive 3 Answers