- Home /
making my gun shoot...
My problem with my gun so far is the bullet. I know i need it to go out a distance and then go back to the gun, but doing that, is over my head. here is the code i saw and put on my bullet: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class bulletscript : MonoBehaviour {
var Bullet : Transform;
var Spawn : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
function Shot()
{
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 8000);
}}
so the error it gives it the :'s in the beginning of the code, and I tried using ='s but I have no idea what i am doing. anyway, thanks!
Answer by Jinkata · Jul 12, 2017 at 06:08 PM
Looks like your file is a C# file and you've written your code in JavaScript.
Even with that fixed you're calling Shoot()
but your function name is Shot()
Here's the C# equivalent:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bulletscript : MonoBehaviour {
public Transform Bullet;
public Transform Spawn;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 8000);
}
}
Answer by Steel598 · Jul 12, 2017 at 06:34 PM
@Jinkata it said it is using obsolete api's... it is on line 20. is there another way to do that?
@Steel598 This should really be a comment rather than an answer, but it looks like this is due to the pel.rigidbody
. Seems like the preferred method is to now use GetComponent
so you would do something like this:
pel.GetComponent<Rigidbody>().AddForce(transform.forward * 8000);
OR
var rb = pel.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 8000);
Either one will work.
https://docs.unity3d.com/2017.2/Documentation/ScriptReference/GameObject-rigidbody.html
Answer by kikonavpash · Sep 19, 2017 at 07:31 PM
what buton on the keyboard is "fire 1" i dont know it
@kikonavpash go into Edit up on the top left then go down to project settings and then input then click on Fire1 and set your button if not already set its default is usually L$$anonymous$$B or left $$anonymous$$ouse Button.