This version of the spaceship script works in Unity 2018

Forum Archive This version of the spaceship script works in Unity 2018

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7873
    stef_7
    Participant

      I had problems making the spaceship script work (haven’t tried the bullet script yet). Here are the changes I’ve done to the script to make it work for me in Unity 2018 :

      – the rigidbody2D.AddForce()… line is not working in Unity 2018. I’ve changed this line and added 2 other lines (look for them in the vicinity of the void Start() line)
      –  my spaceship was jumping to one corner of the screen just after the start of the game. The reason was the values for screenSW and screenNE were inverted so I changed the lines were they get their values and it fixed the problem.  EDIT : no need to do this change, problem was my camera was not set properly, it needs to be set to ‘orthographic’

      Here’s the whole script.

      ******************************************************************

      using
      UnityEngine;

      using
      System.Collections;

      using
      System.Collections.Generic;

      public
      class
      Spaceship
      : MonoBehaviour {

          public
      GameObject spaceshipBulletPrefab;

          public
      float
      speed =  1f;

          public
      float
      turnSpeed = 1f;

          public
      float
      fireRate = 0.5f;

          private
      float
      accelRate = 0f;

          private
      Animator animator;

          private
      Vector3 screenSW;

          private
      Vector3 screenNE;

          private
      float
      wrapPadding = 1f;

          private
      bool
      hit = false;

          private
      float
      nextFire;

         public
      Rigidbody2D rb2D;

         // Use this for initialization

         void
      Start
      () {

              animator = GetComponent<Animator>();

             rb2D = GetComponent<Rigidbody2D>();

             screenNE = Camera.main.ScreenToWorldPoint(new
      Vector3(0, 0, Camera.main.transform.localPosition.z));

       

             screenSW = Camera.main.ScreenToWorldPoint(new
      Vector3(Screen.width, Screen.height, Camera.main.transform.localPosition.z));

         

         }

          

          // Update is called once per frame

          void
      Update
      () {

              float
      translation = Input.GetAxis(“Vertical”);

              float
      rotation = Input.GetAxis(“Horizontal”);

              if(rotation > 0){

                  TurnRight(rotation);

              }

              else
      if(rotation < 0){

                  TurnLeft(rotation);

              }

              if(translation >= 0.5f){

                  Move(translation);

              }

              else{

                  Idle();

              }

              if(Input.GetButton(“Jump”)){

                  ShootBullet();

              }

             rb2D.AddForce(transform.up * (speed * accelRate));

             

             if
      (transform.localPosition.x < screenSW.x – wrapPadding){

                  transform.localPosition = new
      Vector3(screenNE.x, transform.localPosition.y, transform.localPosition.z);

              }

              else
      if(transform.localPosition.x > screenNE.x + wrapPadding){

                  transform.localPosition = new
      Vector3(screenSW.x, transform.localPosition.y, transform.localPosition.z);

              }

              if(transform.localPosition.y < screenSW.y – wrapPadding){

                  transform.localPosition = new
      Vector3(transform.localPosition.x, screenNE.y, transform.localPosition.z);

              }

              else
      if(transform.localPosition.y > screenNE.y + wrapPadding){

                  transform.localPosition = new
      Vector3(transform.localPosition.x, screenSW.y, transform.localPosition.z);

              }

             

          }

          public
      void
      TurnRight(float
      rotation = 1){

              if(hit){

                  return;

              }

              transform.Rotate(Vector3.back * (rotation * turnSpeed));

          }

          public
      void
      TurnLeft(float
      rotation = 1){

              if(hit){

                  return;

              }

              

              transform.Rotate(Vector3.forward * (rotation * -turnSpeed));

          }

          public
      void
      Move(float
      accel){

              if(hit){

                  return;

              }

              accelRate = accel;

              animator.SetInteger(“State”, 1);

          }

          public
      void
      Idle(){

              if(hit){

                  return;

              }

              accelRate = accelRate * 0.5f;

              animator.SetInteger(“State”, 0);

          }

          public
      void
      ShootBullet(){

              if(hit){

                  return;

              }

              if(Time.time > nextFire){

                  nextFire = Time.time + fireRate;

                  Instantiate(spaceshipBulletPrefab, transform.localPosition, transform.localRotation);

              }

          }

      }

      #27782
      Jonathan Gonzalez
      Participant

        Great!  I may need to go through the project files to see what needs to be updated. From time to time Unity changes the script API and usually  makes it easy on you by updating the script for you. Sometimes you’ll see a popup say that Unity will update your scripts and it’ll add that kind of stuff in. 

        #27783
        stef_7
        Participant

          Yes, I’ve seen this kind of pop up at times. My guess is it’s more likely to happen if the version of Unity is not too different from the old one (maybe just one or two generations more recent).

          Anyway, I’m more than halfway into the tutorial now and that’s the only change I had to do (btw, this is a very nice project to do 🙂  ).

          About the lines where I swapped the definitions of screenSW and screenNE, the problem was not the script but my camera. It was set to perspective instead of orthographic.

        Viewing 3 posts - 1 through 3 (of 3 total)
        • The topic ‘This version of the spaceship script works in Unity 2018’ is closed to new replies.