Search Unity

Space Shooter-How do I make a gameobject move and then stop?

Discussion in 'Scripting' started by KDamon, Oct 7, 2017.

  1. KDamon

    KDamon

    Joined:
    Sep 21, 2017
    Posts:
    33
    I'm playing around with a space shooter tutorial I found on the unity website, and I'm trying to make a boss enemy that appears when I reach a certain amount of points. But I can't figure out how to make the boss move to the center of the screen. I think it has something to do with the mover script I made for the other enemies. The other enemies move all the way to the bottom of the screen. How can I make a boss mover script that just has it move half way on the screen?

    Code (CSharp):
    1.  
    2. public class Mover : MonoBehaviour {
    3. public float speed;
    4. private Rigidbody rb;
    5.  
    6. void Start()
    7. {
    8.    rb = GetComponent<Rigidbody>();
    9.    rb.velocity = transform.forward * speed;
    10. }
    11. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The easiest solution would be to clamp the position in FixedUpdate().

    Code (csharp):
    1.  
    2.  
    3. void FixedUpdate()
    4. {
    5.    if(transform.position.y >= whatever point you want)
    6.    {
    7.       transform.position = new Vector3(transform.position.x, whatever point you want, transform.position.z);
    8.    }
    9. }    
    10.  
    Not the ideal solution but will work just fine for you.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Depending on what your game does (or the boss, actually).. if it just stops when it reaches that 'height', you could set the velocity to zero, too.