- Home /
Syntax error in C#
using UnityEngine; using System.Collections; public class shoot : MonoBehaviour { public Rigidbody bullet; public float power = 1500f; public float moveSpeed = 2f;
void Update () {
float h = Input.GetAxis ("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis ("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate (h, v, 0);
if(Input.GetButtonUp("Fire1")){
Rigidbody instance = Instantiate (bullet, Transform.position, Transform.rotation) as Rigidbody;
Vector3 fwd = Transform.TransformDirection (Vector3.forward);
instance.AddForce (fwd * power);
}
}
@Dragix99: Don't post an answer to your question to thank Gruffy for his answer. If the answer solved your question, accept it!!
If you want to reply to the answer, post a comment by clicking on "add new comment" below the post you want to comment on.
Answer by Gruffy · Mar 24, 2014 at 11:35 PM
Hey bud, you were missing a semi- colon at the end of the class and you had used the type declaration of Transform when you should have been addressing the component which is attached to the gameobject you have this script attached to. i have rectified the script and all should compile fine now.
This script needs to go on your gameobject.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour
{
public Rigidbody bullet; public float power = 1500f; public float moveSpeed = 2f;
void Update()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h, v, 0);
if (Input.GetButtonUp("Fire1"))
{
Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
}
Take care dude. Gruffy
It's still giving me issue with if (Input.GetButtonUp("Fire1"))
Okay, this is because you may not have Fire1" specified in tyour input panel.
You must go to "Project Settings -> Input" and either make a new one or grab any spare input option you want and give it the name Fire and check or assign a negative/positive key press assigned to it. I have attached an image to help clarify it for you bud.
Take care dude. Gruffy