Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question CREATE WITH CODE WEEK 2 PROJECT

Discussion in 'Editor & General Support' started by nalar16, Jul 29, 2020.

  1. nalar16

    nalar16

    Joined:
    Jul 26, 2020
    Posts:
    3
    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);
    }
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Your problem almost certainly lies elsewhere. Have you tried removing this script and seeing if it works again?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  4. nalar16

    nalar16

    Joined:
    Jul 26, 2020
    Posts:
    3
    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);
    }
    }
     
  5. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    DestroyOutOfBounds script

    if (transform.position.z < topBound)

    Should read:

    if (transform.position.z > topBound)

    < = less than
    > = greater than
     
    nalar16 likes this.
  6. nalar16

    nalar16

    Joined:
    Jul 26, 2020
    Posts:
    3
    Thanks!!!