Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Shots

Discussion in 'Scripting' started by KarBis, Sep 9, 2019.

  1. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    25
    Hello guys

    I try to create script for shots, but it is not working with any key, eventhough the unity is not displaying any errors in control panel. Could you take a look?

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, yMin, yMax;
    }

    public class PlayerController : MonoBehaviour
    {
    public float speed;
    public float tilt;
    public Boundary boundary;

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;

    private float nextFire;

    void update()
    {
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    }
    }

    void FixedUpdate ()

    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
    GetComponent<Rigidbody>().velocity = movement * speed;


    Thank you!

    GetComponent<Rigidbody>().position = new Vector3
    (

    Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    Mathf.Clamp(GetComponent<Rigidbody>().position.y, boundary.yMin, boundary.yMax)
    );

    GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    }
    }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Debug.Log it!
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,190
    Unity special methods always start with a capital letter. so update should be Update

    And use code tags the next time you post code.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Not wrong, but technically all methods in C# should start with a capital letter. The same goes for class and property names.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,776
    Well, stylistically yes, but the Unity ones are the ones that will make code actually fail to function, as opposed to just hard to read.
     
    Brathnann likes this.
  6. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    25
    Sorry, I'm new, will use tags for the future

    The debug showed " PlayerController.cs(29,23): error CS1002: ; expected
     
  7. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    25
    Ok the upper case for :Update" solved the problem, thanks guys!