[C#] Help me shoot a projectile.

Hello community!:slight_smile:

So, I am trying to make it create a fireball and have the fireball move forward. (like its shooting the fireball)

My code so far:

if (Input.GetKeyDown(KeyCode.Q)) {
				if(Time.time > ability2Cooldown){
					ability2Cooldown = Time.time + 7;
					Rigidbody bulletfire = (Rigidbody)Network.Instantiate(fireBallObj, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity, 0);
					bulletfire.rigidbody.AddForce(transform.forward * 1000);
    				
				} else {
					audio.PlayOneShot(cancelAudio);
				}
			}

No errors until I press Q. Then I get:

It will create the fireball where I am, yet doesn’t shoot or push it forward. Can I get some help please?

Also, my line 59 is:

Try instantiating your “bulletFire” rigidbody with the following line:

Rigidbody bulletfire = Network.Instantiate(fireBallObj, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity, 0) as Rigidbody;

Tried just that and got error:

My line 61:

If there’s a rigidbody on your “fireBallObj” you should be able to use:

bulletfire.AddForce(transform.forward * 1000);

This is possible because you’re storing a reference to the newly instantiated “fireBallObj”'s rigidbody component into the “bulletFire” rigidbody variable you’ve created the line before. The error you’re getting suggests that the “fireBallObj” prefab doesn’t have a rigidbody component attached at the root level.

A litte confused of what you said, since all this vocab is new to me. Yet on my FireBall prefab I do have a rigidbody on it and yes its true we are putting “bulletfire” to a ridigbody.
But I am still confused of why it will not work?

I tried:
bulletfire.AddForce(transform.forward * 1000);
also, and still not working :confused:

That is line 68 also,
bulletfire.AddForce(transform.forward * 1000);

Still need help please!:slight_smile:

What is the type of fireBallObj? You code will only work if it’s type Rigidbody.

If it’s a GameObject instead of a Rigidbody, then you should do this:

GameObject bulletfireObject = Network.Instantiate(fireBallObj, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity, 0) as GameObject;
Rigidbody bulletfire = bulletfireObject.rigidbody;

Well my fireBallObj is a Transform.

public Transform fireBallObj;

And I have:

Rigidbody bulletfire = Network.Instantiate(fireBallObj, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity, 0) as Rigidbody;
					bulletfire.AddForce(transform.forward * 1000);

soo im confused of why its not working?

Also, I attached a prefab of my fireball to the “fireBallObj”

Set your FireBall as a rigidbody and add a spawnPoint as a Transform. Then try this :

var FireBall : Rigidbody;
var spawnPoint : Transform;
var forward Force = 10000;

function Update(){
if(Input.GetButton(“Fire1”)){

var shoot = Instantiate(FireBall,spawnPoint.position,spawnPoint.rotation);
shoot.AddForce(spawnPoint.forward*forward Force);

I’m not familiar with C# sorry.

You can tweak the Forward force, 10000 will probably take it to mars.

Because fireBallObj is type Transform, Network.Instantiate(fireBallObj) returns an object that is a Transform, not a Rigidbody. You cannot cast, or change, a Transform into a Rigidbody.

What Intense_Gamer94 says is right.

// Assigning this in the Inspector will get you the Rigidbody instead of the Transform
public Rigidbody fireBallObj;

Then your code should work as Network.Instantiate(fireBallObj) will return an object that is a Rigidbody, which can then be put into the Rigidbody bulletfire variable.

tl;dr: GameObject, Transform, and Rigidbody are all different things! A variable can only hold a single type, not any type.

Wikipedia shows Earth’s escape velocity as 11 200 m/s. So that’s pretty accurate. :wink:

Thank you all so much!:slight_smile:

It’s working properly!

Can I do something to make it say if it collides with an object to delete it? And maybe deal some damage?

Yes you can! Your bullet should have a script attached to it as well as whatever you want to take damage.

// This script should be attached to your bullet prefab.
using UnityEngine;

[RequireComponent(typeof(RigidBody))]
public class Bullet : MonoBehaviour {
  void OnCollisionEnter(Collision info) {
    // Unity has detected a collision. Did we hit an enemy?
    Enemy e = info.gameObject.GetComponent<Enemy>();
    if (e != null) {
      // Causes 10 damage.
      e.TakeDamage(10);
    }
  }
}
// This script should be attached to enemies who can take damage.
using UnityEngine;

public class Enemy : MonoBehaviour {
    public void TakeDamage(int damageAmount) {
      health -= damageAmount;
      if (health <= 0) {
        // Out of life. Delete self.
        Destroy(gameObject);
      }
    }

    private int health = 100;
}

If you can understand all this code you’re well on your way to “getting” scripting with unity.

Thank you very much! All problems solved :slight_smile: