コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        //if x>0 decrement it until it reaches 0
        if (x > 0)
        {
            x--;
        }
        //quit hotkey
        if (Input.GetKeyDown("q"))
        {
            SceneManager.LoadScene("menu");
        }
        //score string
        s  = "Score: ";
        s += score;
        // update score display
        transform.GetChild(0).GetChild(0).GetChild(1).GetComponent <Text>().text = s;
        //check if touching ground
        ground = Physics.Raycast(transform.position, -transform.up, .5F, 1 << 8);
        //if touching ground, make child of planet it is touching
        if (ground)
        {
            transform.parent = planets[ori].transform;
        }
        //otherwise no parent
        else
        {
            transform.parent = null;
        }
        //reset acceleration vector
        acc = Vector3.zero;
        //get razerhydra input (left controller)
        plugin.getNewestData(left);
        //rotate camera according to controller
        transform.GetChild(0).localEulerAngles = new Vector3(plugin.data.position.y / -8, plugin.data.position.x / 8, 0);
        //if touching ground, rotate based on joystick
        if (ground)
        {
            transform.Translate(Vector3.forward * plugin.data.joystick_y * .2F, Space.Self);
            transform.Rotate(Vector3.up * plugin.data.joystick_x, Space.Self);
        }
        //otherwise tilt based on joystick
        else
        {
            transform.Rotate(Vector3.right * plugin.data.joystick_y * 2, Space.Self);
            transform.Rotate(Vector3.back * plugin.data.joystick_x * 2, Space.Self);
        }
        //if trigger is pressed jump up
        if (plugin.data.trigger > 0.8F)
        {
            acc += transform.up * 20;
        }
        //get data from right controller
        plugin.getNewestData(right);
        //aim gun with right controller
        transform.GetChild(1).localEulerAngles = new Vector3(plugin.data.position.y / -8, plugin.data.position.x / 8, 0);
        //if trigger is pressed
        if (plugin.data.trigger > 0.8F)
        {
            //play fire sound
            au.PlayOneShot(bul, 1);
            //make bullet, point bullet in direction of gun
            Instantiate(bullet, transform.GetChild(1).GetChild(0).GetChild(1).position, Quaternion.identity, transform);
            transform.GetChild(transform.childCount - 1).gameObject.GetComponent <Scr_Bullet>().planets = planets;
            transform.GetChild(transform.childCount - 1).gameObject.GetComponent <Scr_Bullet>().vel     = Vector3.Normalize(transform.GetChild(1).GetChild(0).GetChild(1).position - transform.GetChild(1).GetChild(0).GetChild(0).position) * 10;
        }
        //if not on ground
        if (!ground)
        {
            //add all planets gravity onto player
            for (int i = 0; i < planets.Length; i++)
            {
                //F=constant which is 1*mass/R^2
                //in this case planet position-position/R^3
                acc += planets[i].GetComponent <Rigidbody>().mass *(planets[i].transform.position - transform.position) / Mathf.Pow(Vector3.Distance(transform.position, planets[i].transform.position), 3);
            }
        }
        for (int i = 0; i < planets.Length; i++)
        {
            //player orientation is toward planet with strongest gravity
            if (Mathf.Pow(Vector3.Distance(transform.position, planets[ori].transform.position), 2) / planets[ori].GetComponent <Rigidbody>().mass > Mathf.Pow(Vector3.Distance(transform.position, planets[i].transform.position), 2) / planets[i].GetComponent <Rigidbody>().mass)
            {
                ori = i;
            }
        }

        //find orientation based on forward and planet gravity vector
        orientation = Quaternion.LookRotation(Vector3.Cross(transform.position - planets[ori].transform.position, Vector3.Cross(transform.forward, transform.position - planets[ori].transform.position)), transform.position - planets[ori].transform.position);
        //if not on ground rotate player towards orientatation
        //player should be upright from planet which gravity is strongest
        if (!ground || Quaternion.Angle(orientation, transform.rotation) > .05F)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, orientation, Quaternion.Angle(orientation, transform.rotation) * .1F);
        }
        //keyboard input incase no razer hydra
        //jump
        if (Input.GetKeyDown("space"))
        {
            acc += (transform.up + transform.forward) * 40;
        }
        //fire
        if (Input.GetKey("p") && x == 0)
        {
            x = 3;
            au.PlayOneShot(bul, 1);
            Instantiate(bullet, transform.GetChild(1).GetChild(0).GetChild(1).position, Quaternion.identity, transform);
            transform.GetChild(transform.childCount - 1).gameObject.GetComponent <Scr_Bullet>().planets = planets;
            transform.GetChild(transform.childCount - 1).gameObject.GetComponent <Scr_Bullet>().vel     = Vector3.Normalize(transform.GetChild(1).GetChild(0).GetChild(1).position - transform.GetChild(1).GetChild(0).GetChild(0).position) * 10 + self.velocity;
        }
        //movement
        if (ground)
        {
            if (Input.GetKey("w"))
            {
                transform.Translate(Vector3.forward * .05F, Space.Self);
            }
            if (Input.GetKey("s"))
            {
                transform.Translate(Vector3.back * .05F, Space.Self);
            }
            if (Input.GetKey("a"))
            {
                transform.Rotate(Vector3.down, Space.Self);
            }
            if (Input.GetKey("d"))
            {
                transform.Rotate(Vector3.up, Space.Self);
            }
        }
        else
        {
            if (Input.GetKey("w"))
            {
                transform.Rotate(Vector3.right * 2, Space.Self);
            }
            if (Input.GetKey("s"))
            {
                transform.Rotate(Vector3.left * 2, Space.Self);
            }
            if (Input.GetKey("a"))
            {
                transform.Rotate(Vector3.forward * 2, Space.Self);
            }
            if (Input.GetKey("d"))
            {
                transform.Rotate(Vector3.back * 2, Space.Self);
            }
        }
        //add acceleration to velocity
        self.velocity += acc;
        //drag
        self.velocity = self.velocity * .95F;
    }
コード例 #2
0
 void move()
 {
     plugin.getNewestData(0);
     playerTransform.Translate((plugin.data.joystick_x) / 25f, 0f, plugin.data.joystick_y / 25f);
 }