My shots don't want to move!
I think, that many did official tutorial. But I'm stuck in lesson number 7 ("Space shooter") and I do not know what's the matter :( Error: UnassignedReferenceException: The variable rb of Mover has not been assigned. You probably need to assign the rb variable of the Mover script in the inspector.
My code: using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
public float speed, tilt;
public Boundary boundary;
public GameObject shot;
public Transform Shot_spawn;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
GameObject clone =
Instantiate(shot, Shot_spawn.position, Shot_spawn.rotation) as GameObject;
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3
(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
}
}
I'm using version 5.6!
Looks like you forgot to give your shots an initial velocity when spawning them. If you just spawn them, they will just stay there, you need to give them a speed. The answer by Chounr is what you're looking for to do that.
Answer by Chounr · Aug 23, 2017 at 07:29 AM
From the tutorial, you need to add another script call Mover to the Bolt Prefabs object.
public float speed;
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
rb.velocity = transform.forward * speed;
}
Your answer
Follow this Question
Related Questions
The bolt doesn't move 0 Answers
[tutorial - Space Shooter] about smoothing in EvasiveManeuver.cs 2 Answers
Sphere with rigidbody and playercontroller script stuck in plane and wont move 0 Answers
My bolt isn't respawning in game Its driving me crazy i have been at it for 2 days !! 1 Answer
Space Shooter Tutorial - Explosions not appearing when objects are destroyed. 0 Answers