hello i’m doing a tutorial game of space shooter and that’s the code , i have 4 error can you pls help me out to find out the code is exactly as the tutorial ,
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xmin,xmax,zmin,zmax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
public GameObject Shot;
public Transform ShotSpawn;
public float FireRate;
private float nextfire;
void update()
{
if (Input.getbutton(“fire1”) && Time.time > next.fire)
next.fire=Time.time+FireRate;
//gameobject clone
Instantiate(Shot,ShotSpawn.position,ShotSpawn.rotation);//as GameObject;
}
}
void FixedUpdate()
{
float moveHorizontal =Input.GetAxis("Horizontal");
float moveVertical =Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
GetComponent<Rigidbody>().velocity= movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x,boundary.xmin,boundary.xmax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z,boundary.zmin,boundary.zmax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f,0.0f,GetComponent<Rigidbody>().velocity.x*-tilt);
}
errors
Assets/Scripts/PlayerController.cs(54,1): error CS8025: Parsing error
Assets/Scripts/PlayerController.cs(33,14): error CS0116: A namespace can only contain types and namespace declarations
I don’t even know where too start.
The error you got was because FixedUpdate() is outside of class (as the error says)
" update() " should be called " Update() "
You have a float " nextfire " and you trying to reference it as " next.fire "
" getbutton " should be " GetButton "
" fire1 " should be " Fire1 " (unless you changed it in Input Manager then Sorry and ignore this)
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xmin,xmax,zmin,zmax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
public GameObject Shot;
public Transform ShotSpawn;
public float FireRate;
private float nextfire;
void Update() {
if (Input.GetButton("fire1") && Time.time > nextfire)
nextfire=Time.time+FireRate;
//gameobject clone
Instantiate(Shot,ShotSpawn.position,ShotSpawn.rotation);//as GameObject;
}
void FixedUpdate() {
float moveHorizontal =Input.GetAxis("Horizontal");
float moveVertical =Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
GetComponent<Rigidbody>().velocity= movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x,boundary.xmin,boundary.xmax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z,boundary.zmin,boundary.zmax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f,0.0f,GetComponent<Rigidbody>().velocity.x*-tilt);
}
}
Resolved , but the shots stays in the spawn position and don’t go forward the Z axis , can some1 explain me why?
Here the code , using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
public GameObject shot;
public GameObject shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
shot = Instantiate(shot) as GameObject;
nextFire = Time.time + fireRate;
//GameObject clone =
shot.transform.position = shotSpawn.transform.position;
}
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}
and this is the Bullet code script
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed;
void Start ()
{
GetComponent<Rigidbody>().velocity = transform.forward*speed;
}
}