Everything worked fined until this script was added. Now, the PlayerController do not move from left to right and pizza do not move. HELP!!!
public class DestroyOutOfBounds : MonoBehaviour
{
private float topBound = 30;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
//Destroys object when out of bounds
{
if (transform.position.z < topBound)
{
Destroy(gameObject);
}
}
}
Your problem almost certainly lies elsewhere. Have you tried removing this script and seeing if it works again?
1 Like
This posted script does nothing to stop movement. All it is capable of doing is destroying the GameObject it is attached to if the condition in Update is met. Maybe the GameObject it is attached to has something to do with the movement of your player or “pizza”? No idea, since you’ve been fairly light on the details of the issue.
Thanks for the help. I’ve deleted the script and it runs as before. Maybe I’m missing something with the PlayerController script or the MoveForward script for the objects.
public class PlayerController : MonoBehaviour
{
public float horizontalInput;
public float speed = 10.0f;
public float xRange = 10.0f;
public GameObject projectilePrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Move PlayerController horizontally within a range
if (transform.position.x < -xRange)
{
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
}
if (transform.position.x > xRange)
{
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
}
horizontalInput = Input.GetAxis(“Horizontal”);
transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
//Use spacebar to so PlayerController fires projectile
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
}
}
_______________________________________
public class MoveForward : MonoBehaviour
{
public float speed = 40.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
// Moves object forward
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
DestroyOutOfBounds script
if (transform.position.z < topBound)
Should read:
if (transform.position.z > topBound)
< = less than
= greater than
1 Like