- Home /
How to make arrow stick to environment ?
Hi All,
I am currently making a mobile game. the game is an archery game, where you use the gyro in your phone to aim and tap the screen to shoot. I am currently having trouble with getting my arrows to stick to the target board? Any ideas ?
void Update()
{
if (Input.touchCount > 0 && Time.time > nextFire)
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Fire();
}
}
nextFire = Time.time + fireRate;
}
}
void Fire()
{
var Arrow = (GameObject)Instantiate(Arrows, ArrowSpawn.position, ArrowSpawn.rotation);
Arrow.GetComponent<Rigidbody>().velocity = Camera.main.transform.forward * Velocity;
//Destroy(Arrow, 5.0f);
}
void Oncollisionenter(Collision collision) { //Need to some how get instantiated arrows and make the arrow a child of the target? //or maybe make it kinematic(however, i wanna make moving targets so would cause problems) // or just stop velocity
}
Answer by calebmaskew · Nov 20, 2016 at 03:43 PM
If just making the arrow a child of the target is your main issue, the easiest way I've found to do so is to set the parent of the arrow's transform to the transform of the target. This would be in your arrow behavior script:
void OnCollisionEnter(Collider other){
//other here being the target
transform.parent = other.gameObject.transform;
}
In this case, there shouldn't be a need to destroy the arrow, and making it kinematic should be considered if you don't want it to be affected by further physics forces.
Answer by Dibbie · Nov 20, 2016 at 03:23 PM
On collision of your arrow, you could do a few things: 1. Set the velocity of the arrow to zero 2. Turn off "use gravity" (or the entire Rigidbody) 3. Freeze all the constraints of the Rigidbody
Doing 1 and 2, or 1 and 3, essentially pauses/removes the physics that makes it move, so it appears "stuck" where it landed
For moving targets, youd then want to do a combo of those steps as mentioned, but then also parent the arrow to the collision game object hit.
Answer by 2kamrd15 · Nov 21, 2016 at 07:21 AM
Thank you both, I manged to figure it out and i did exactly that. On collision i made the arrow kinematic and the made it a child of the object, allowing me to also make targets move while the arrow is still attached.
Would you have any idea how I could implement, drag down and release to adjust power. So when i drag down it would create a force and then on release it would release?? I was think that this might be down with using the different touch phases @Dibbie @calebmaskew