How to get AI to throw object over obstacles
What I am trying to do is to get the the AI to throw a grenade over an obstacle to its target. What I have so far is just having the AI throw a grenade at the target which works fine but I have no idea how to get it to think about the obstacles that are in the way like in the picture shown below. I want A to notice there is a wall in the middle and throw the object over it to be.
Vector2 diff = target.position - this.character.transform.position;
Vector2 grenadeVel = new Vector2();
grenadeVel.x = diff.x / grendadeDetonationTime ;
grenadeVel.y = (diff.y - 0.5f * worldGravity.y * grendadeDetonationTime * grendadeDetonationTime ) / grendadeDetonationTime ;
Debug.Log(grenadeVel);
World.Instance.WeaponManager.Grenade.Throw(this.character, grenadeVel);
Im not really asking for code (although it would be great), what I want is some advice/ tips on how to implement this and what the logic might be like. Currently my game world is in 2D and it has a grid which I use for path finding with the A* algorithm, is it possible to do what I want using a world grid to create an arch over obstacles?
You may be able to use a Raycast to detect obstacles in front of the AI, and then have it react differently, ie. throw the grenade higher.
Raycast to find objects in the path. If height of objects are a concern, you could detect how tall it is before changing the trajectory. In a situation where you would face a wall or a building, this would be a good idea. You can usually throw over a wall, but can't throw over some tall buildings. This could cue another part of the AI to move to a position where it is reasonable to carry out the action.
An easy way to do it is just hardcode in trajectories if it is flat land same height walls. This gets all wonky once you start changing relative elevation to obstacles though. i.e standing lower than a wall that is on top of a hill.
Answer by suark · Nov 25, 2015 at 09:18 AM
As ZefanS said, First make a raycast from ai player to target area. if raycast hits and object, change the angle (http://docs.unity3d.com/ScriptReference/Quaternion.AngleAxis.html) of the raycast till it doesn't hit anything. Thats the point AI should throw the ball over.
Thanks! That is really useful to know :D one other thing, would you happen to know of an algorithm that I would be able to use to throw the object over the obstacle with the right amount of Velocity to get to the target with the angle data I receive from Raycasting?
If you are using physics try this https://en.wikipedia.org/wiki/Trajectory_of_a_projectile.
Your answer
![](https://koobas.hobune.stream/wayback/20220612050107im_/https://answers.unity.com/themes/thub/images/avi.jpg)