I want to make a way to shoot an arrow when i press the space bar in the direction my player is facing and make it disappear when it collides with something. How do i do this?
There are, as given by others here, a bunch of ways to do this but…
Without overly convoluting it; I will post a script that makes some assumptionsfor you…
Firstly your scene setup will contain a GameObject that uses the script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootFromDirection : MonoBehaviour
{
[SerializeField][Tooltip("place a childed transform component in here, and use its rotation values to determine which direction it will fire from")]
Transform orientation;
[SerializeField][Tooltip("Add a projectile prefab, this is a game object that has a Rigidbody and a Collider set to Trigger and the script named Projectile")]
GameObject projectilePrefab;
[SerializeField][Tooltip("will identify if you can fire")]
bool canFire = true;
private void Update()
{
if(Input.GetKeyUp(KeyCode.Space)) {
if (canFire)
{
canFire= false;
FireProjectile();
}
}
}
private void FireProjectile() {
Instantiate(projectilePrefab, orientation.position, orientation.rotation);
//this projectilePrefab will take care of the moving and collision after being instanced:)
//you may want something like a very simple cool down...
StartCoroutine(CoolDown());
}
private IEnumerator CoolDown() {
yield return new WaitForSeconds(2f);
canFire = true;
}
}
After this, you will have to make a projectile prefab that the above script consumes and that uses the following script:
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class Projectile : MonoBehaviour
{
public float power = 1500f;
public float moveSpeed = 2f;
private Rigidbody rb;
Vector3 fwd;
private void Start() {
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate() {
fwd = transform.TransformDirection(Vector3.forward);
rb.AddForce(fwd * power * moveSpeed);
}
}
here is an overall screenshot to show the hierarchy and Inspector to set this up visually for yourself…
So the outcome here, if you followed along with ease, is that you can now rotate the “Orientation” game object in your scene and have the projectile fire in any direction you wish - it is not restricted to cannon directions, you may change the values for x, y and z and the direction will reflect the rotation you set there.
Hope that’s a start and it helps, if there’s any questions or if unsure of setup: just comment back and I can help you get that bit done as well.
Cheers, happy coding
EDIT: here is a zipped unitypackage that is set up according to my Answer here and for your ease of getting going.
[204970-reliableprojectileshootingwithrb.zip|204970]
Hey,
I’d recommend reading up on the following sections to help you get to grips with getting this working:
1. Prefabs: You could create a prefab of your arrow gameobject.
2. Instantiation: You could then instantiate your arrow prefab.
3. Rigidbodies: Attach a rigidbody to your arrow prefab and when it’s instantiated, apply an impulsive force to it.
4. Collision: Attach a collider to your arrow prefab and detect when it’s collided with something using the OnCollisionEnter method.
5. Destroying: I would call the Destroy method to make the arrow disappear.
I’ve tried not to put too much code here as I find it easier to learn by trying stuff than someone telling me how to do it.
Hope this helped =)
To get the direction of your character, you can use your character’s transform.Forward.
The movement depends on how you are moving the arrow (like Transform or Rigidbody physics movement), but you can use your transform.Forward to guide it. For instance, you might be adding a force to your Rigidbody like arrowRigidBody.AddForce(characterFoward * shootPower); .
You can use OnCollisionEnter method that Unity provides, assuming there is a collider on the object, to do determine when to call “Destroy()” on your arrow, as well as deal damage, make sounds, etc…
