Search Unity

Space Shooter tutorial: C# script 1 not moving the player January 2018.

Discussion in 'Editor & General Support' started by SKYBORG_CEO, Jan 4, 2018.

?

MonoDevelop Scripting Edit

  1. Earth Change?

    0 vote(s)
    0.0%
  2. Software Change?

    0 vote(s)
    0.0%
  1. SKYBORG_CEO

    SKYBORG_CEO

    Joined:
    Dec 21, 2017
    Posts:
    7
    From
    Moving the player
    Checked with version: 5.1
    -
    Difficulty: Beginner

    Writing simple C# code to capture player input and move the player ship.
    https://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player?playlist=17147

    I followed the video and just decided to copy and paste the script to the MonoDevelop script editor.

    It's giving me a lot of errors:



    https://imgur.com/a/6GwfF

    Also, give me some understanding for the right code. is there an area where I can get codes "fixed up" since I'm a newbie? I would really like to work on a pipeline like area even if it's "open-sourced".

    Thank you very much.
     

    Attached Files:

  2. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    rigidbody is deprecated, now you need to use GetComponent https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    Optimized would be cached rigidbody on awake, so it would look like this:

    Code (CSharp):
    1.     [System.Serializable]
    2.     public class Boundary
    3.     {
    4.         public float xMin, xMax, zMin, zMax;
    5.     }
    6.  
    7.     public class PlayerController : MonoBehaviour
    8.     {
    9.         public float speed;
    10.         public float tilt;
    11.         public Boundary boundary;
    12.         Rigidbody rigidbody; // this line added
    13.  
    14.         void Awake()
    15.         {
    16.             rigidbody = GetComponent<Rigidbody>(); // and this added
    17.         }
    18.  
    19.         void FixedUpdate()
    20.         {
    21.             float moveHorizontal = Input.GetAxis("Horizontal");
    22.             float moveVertical = Input.GetAxis("Vertical");
    23.  
    24.             Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    25.             rigidbody.velocity = movement * speed;
    26.  
    27.             rigidbody.position = new Vector3
    28.             (
    29.                 Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
    30.                 0.0f,
    31.                 Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
    32.             );
    33.  
    34.             rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    35.         }
    36.     }
    37. }
     
  3. SKYBORG_CEO

    SKYBORG_CEO

    Joined:
    Dec 21, 2017
    Posts:
    7
    This works with adding on:

    using UnityEngine;
    using System.Collections;

    I am not 100% sure on the rigid body concept so I will be training in C# in a limited amount. I'll look for another building type later.

    Thank you.