I went on Googling on how to fire a bullet in 3D space, since firing one in 2D would be same, just with different coordinates. The script I found (original):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BulletItself : MonoBehaviour {
//the object that will be spawned
public GameObject bulletPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")){//when the left mouse button is clicked
print ("1");//print a message to act as a debug
FireBullet();//look for and use the fire bullet operation
}
}
public void FireBullet(){
//Clone of the bullet
GameObject Clone;
//spawning the bullet at position
Clone = (Instantiate(bulletPrefab, transform.position,transform.rotation)) as GameObject;
Debug.Log ("Bullet is found");
//add force to the spawned objected
Clone.rigidbody.AddForce(1000,0,0);
Debug.Log ("Force is added");
}
}
So I instantly tried to modify it and clean it up (remove comments and remove the unnecessary [Enter]‘s).
But then what I see in MonoDevelop is:
Clone.rigidbody.AddForce(1000,0,0);
“AddForce” creates an error, MonoDevelop says
"UnityEngine.Component does not contain definition of AddForce
.
Tried to fix it, by:
Clone.GetComponent.AddForce(1000,0,0);
But the error I get from it is: “?”, nothing else, just a funny question mark. So I went back to Unity, and it says:
"Assets/Scripts/Player/BulletItself.cs(24,23): error CS0119: Expression denotes a method group', where a
variable’, value' or
type’ was expected". (24,23), points exactly to this line.
And I’m so lost. Is there no easy way to do this? I’m not one of those that just copy, I copy, modify and learn, but heck, so many nice outdated too-specific JavaScript tutorials on how to do this, and not a single would be just “paste this script, put “Bullet” into slot, play with it until you learn, expand the code yourself”.
Back to the problem, how can I resolve it?