Ejemplo n.º 1
0
    void FixedUpdate()
    {
        VCAnalogJoystickBase moveJoystick = VCAnalogJoystickBase.GetInstance("MoveJoyStick");
        VCButtonBase         actionButton = VCButtonBase.GetInstance("Action");
        Vector2 directionVector           = new Vector2(moveJoystick.AxisX, moveJoystick.AxisY);

        if (directionVector != Vector2.zero)
        {
            // Get the length of the directon vector and then normalize it
            // Dividing by the length is cheaper than normalizing when we already have the length anyway
            var directionLength = directionVector.magnitude;
            directionVector = directionVector / directionLength;

            // Make sure the length is no bigger than 1
            directionLength = Mathf.Min(1.0f, directionLength);

            // Make the input vector more sensitive towards the extremes and less sensitive in the middle
            // This makes it easier to control slow speeds when using analog sticks
            directionLength = directionLength * directionLength;

            // Multiply the normalized direction vector by the modified length
            directionVector = directionVector * directionLength;
        }

        GetComponent <NetworkView>().RPC("SendInput", RPCMode.Server, directionVector.x, directionVector.y,
                                         actionButton.Pressed, Input.touchCount > 0);
    }
Ejemplo n.º 2
0
 // Initialization of UI control
 private void InitUIControl()
 {
     cameraControl = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <CameraController>();
     joyStick      = GameObject.Find("VCAnalogJoystick").GetComponent <VCAnalogJoystickBase>();
     bars          = GetComponentsInChildren <Image>();
     healthBar     = bars[0];
     happinessBar  = bars[1];
 }
    // not implemented as they aren't very popular,
    // uncomment here and in Update() if you want to use

    //public float magnitudeSqr;
    //public float angleRadians;
    //public float angleDegrees;
    #endregion

    void Start()
    {
        if (joystick == null)
        {
            // try to find it on this gameObject.
            joystick = gameObject.GetComponent <VCAnalogJoystickBase>();
            if (joystick == null)
            {
                VCUtils.DestroyWithError(gameObject, "You must specify a joystick for VCAnalogJoystickPlaymakerUpdater to function.  Destroying this object.");
                return;
            }
        }
    }
Ejemplo n.º 4
0
        public void InitUIControl()
        {
            if (!GameController.GetSceneName().Equals("Farm"))
            {
                return;
            }

            userInterface = GameObject.Find("UserInterface").GetComponent <FarmUI>();
            joyStick      = GameObject.Find("VCAnalogJoystick").GetComponent <VCAnalogJoystickBase>();
            cameraControl = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <CameraController>();
            playerGO      = GameObject.Find("Player");
            movement      = playerGO.GetComponent <Movement>();
        }
Ejemplo n.º 5
0
    void OnGUI()
    {
        // if there's an analog joystick, output some info
        if (VCAnalogJoystickBase.GetInstance("stick") != null)
        {
            GUI.Label(new Rect(10, 10, 300, 20), "Joystick Axes: " + VCAnalogJoystickBase.GetInstance("stick").AxisX + " " + VCAnalogJoystickBase.GetInstance("stick").AxisY);
        }

        // if there's an a button, output some info
        if (VCButtonBase.GetInstance("A") != null)
        {
            GUI.Label(new Rect(10, 30, 300, 20), "Button Hold (s): " + VCButtonBase.GetInstance("A").HoldTime.ToString());
        }

        // if there's a dpad, output some info
        VCDPadBase dpad = VCDPadBase.GetInstance("dpad");

        if (dpad != null)
        {
            string str = "DPad: ";
            if (dpad.Left)
            {
                str += "Left ";
            }
            if (dpad.Right)
            {
                str += "Right ";
            }
            if (dpad.Up)
            {
                str += "Up ";
            }
            if (dpad.Down)
            {
                str += "Down ";
            }
            if (dpad.Pressed(VCDPadBase.EDirection.None))
            {
                str += "(No Direction)";
            }

            GUI.Label(new Rect(10, 50, 300, 20), str);
        }

        GUI.Label(new Rect(10, 70, 300, 20), "Move cube using controls");
        GUI.Label(new Rect(10, 90, 300, 20), "Double tap joystick / Press A for particles");
    }
Ejemplo n.º 6
0
    void OnGUI()
    {
        // if there's an analog joystick, output some info
        if (VCAnalogJoystickBase.GetInstance("stick") != null)
        {
            GUI.Label(new Rect(10, 10, 300, 20), VCAnalogJoystickBase.GetInstance("stick").AxisX + " " + VCAnalogJoystickBase.GetInstance("stick").AxisY);
        }

        // if there's an a button, output some info
        if (VCButtonBase.GetInstance("BtnA") != null)
        {
            GUI.Label(new Rect(10, 30, 300, 20), "aaaaaaaaaa");
        }
        else
        {
            GUI.Label(new Rect(10, 30, 300, 20), "nullllll");
        }

        // if there's a dpad, output some info
        VCDPadBase dpad = VCDPadBase.GetInstance("dpad");

        if (dpad != null)
        {
            string str = "";
            if (dpad.Left)
            {
                str += "Left ";
            }
            if (dpad.Right)
            {
                str += "Right ";
            }
            if (dpad.Up)
            {
                str += "Up ";
            }
            if (dpad.Down)
            {
                str += "Down ";
            }

            GUI.Label(new Rect(10, 50, 300, 20), str);
        }

        GUI.Label(new Rect(10, 70, 300, 20), "Move cube using controls");
        GUI.Label(new Rect(10, 90, 300, 20), "Double tap joystick / Press A for particles");
    }
Ejemplo n.º 7
0
    void Update()
    {
        // Use the DPad to move the cube container
        if (cubeContainer)
        {
            // try and get the dpad
            VCDPadBase dpad = VCDPadBase.GetInstance("dpad");

            // if we got one, perform movement
            if (dpad)
            {
                if (dpad.Left)
                {
                    cubeContainer.transform.Translate(-moveSpeed * Time.deltaTime, 0.0f, 0.0f);
                }
                if (dpad.Right)
                {
                    cubeContainer.transform.Translate(moveSpeed * Time.deltaTime, 0.0f, 0.0f);
                }
                if (dpad.Up)
                {
                    cubeContainer.transform.Translate(0.0f, moveSpeed * Time.deltaTime, 0.0f);
                }
                if (dpad.Down)
                {
                    cubeContainer.transform.Translate(0.0f, -moveSpeed * Time.deltaTime, 0.0f);
                }
            }

            // do the same for the analog joystick
            VCAnalogJoystickBase joy = VCAnalogJoystickBase.GetInstance("stick");
            if (joy != null)
            {
                cubeContainer.transform.Translate(moveSpeed * Time.deltaTime * joy.AxisX, moveSpeed * Time.deltaTime * joy.AxisY, 0.0f);
            }
        }

        // rotate the cube for coolness effect
        if (cube)
        {
            cube.transform.RotateAroundLocal(new Vector3(1.0f, 1.0f, 0.0f), Time.deltaTime);
        }

        // and emit particles based on the time we've held down our A button (if we have one)
        VCButtonBase abtn = VCButtonBase.GetInstance("A");

        if (abtn != null && cubeContainer)
        {
            ParticleSystem particles = cubeContainer.GetComponentInChildren <ParticleSystem>();
            if (particles != null)
            {
                particles.emissionRate = abtn.HoldTime * 50.0f;
            }

            // emit some particles whenever joystick is double clicked
            VCAnalogJoystickBase joy = VCAnalogJoystickBase.GetInstance("stick");
            if (joy != null && particles != null && joy.TapCount > 1)
            {
                particles.emissionRate = 150.0f;
            }
        }

        // example of how to detect if press began or ended exactly on this frame
        if (abtn != null)
        {
            if (abtn.PressBeganThisFrame)
            {
                Debug.Log("Press began on frame " + Time.frameCount);
            }

            if (abtn.PressEndedThisFrame)
            {
                Debug.Log("Press ended on frame " + Time.frameCount);
            }
        }
    }
Ejemplo n.º 8
0
    private void Update()
    {
        var movement = thisTransform.TransformDirection(new Vector3(moveTouchPad.AxisX, 0.0f, moveTouchPad.AxisY));

        // We only want horizontal movement
        movement.y = 0.0f;
        movement.Normalize();

        // Apply movement from move joystick
        var absJoyPos = new Vector2(Mathf.Abs(moveTouchPad.AxisX), Mathf.Abs(moveTouchPad.AxisY));

        if (absJoyPos.y > absJoyPos.x)
        {
            if (moveTouchPad.AxisY > 0.0f)
            {
                movement *= forwardSpeed * absJoyPos.y;
            }
            else
            {
                movement *= backwardSpeed * absJoyPos.y;
            }
        }
        else
        {
            movement *= sidestepSpeed * absJoyPos.x;
        }

        // Check for jump
        if (character.isGrounded)
        {
            var jump = false;

            VCAnalogJoystickBase touchPad = rotateTouchPad;

            if (rotateTouchPad == null)
            {
                touchPad = moveTouchPad;
            }

            if (!touchPad.Dragging)
            {
                canJump = true;
            }

            if (canJump && touchPad.TapCount >= 2)
            {
                jump    = true;
                canJump = false;
            }

            if (jump)
            {
                // Apply the current movement to launch velocity
                velocity   = character.velocity;
                velocity.y = jumpSpeed;
            }
        }
        else
        {
            // Apply gravity to our velocity to diminish it over time
            velocity.y += Physics.gravity.y * Time.deltaTime;

            // Adjust additional movement while in-air
            movement.x *= inAirMultiplier;
            movement.z *= inAirMultiplier;
        }

        movement += velocity;
        movement += Physics.gravity;
        movement *= Time.deltaTime;

        // Actually move the character
        character.Move(movement);

        if (character.isGrounded)
        {
            // Remove any persistent velocity after landing
            velocity = Vector3.zero;
        }

        // Apply rotation from rotation joystick
        if (character.isGrounded)
        {
            var camRotation = Vector2.zero;

            if (rotateTouchPad != null)
            {
                camRotation.x = rotateTouchPad.AxisX;
                camRotation.y = rotateTouchPad.AxisY;
            }
            else
            {
                // Use tilt instead
                //			print( iPhoneInput.acceleration );
                var acceleration = Input.acceleration;
                var absTiltX     = Mathf.Abs(acceleration.x);
                if (acceleration.z < 0.0f && acceleration.x < 0.0f)
                {
                    if (absTiltX >= tiltPositiveYAxis)
                    {
                        camRotation.y = (absTiltX - tiltPositiveYAxis) / (1.0f - tiltPositiveYAxis);
                    }
                    else if (absTiltX <= tiltNegativeYAxis)
                    {
                        camRotation.y = -(tiltNegativeYAxis - absTiltX) / tiltNegativeYAxis;
                    }
                }

                if (Mathf.Abs(acceleration.y) >= tiltXAxisMinimum)
                {
                    camRotation.x = -(acceleration.y - tiltXAxisMinimum) / (1.0f - tiltXAxisMinimum);
                }
            }

            camRotation.x *= rotationSpeed.x;
            camRotation.y *= rotationSpeed.y;
            camRotation   *= Time.deltaTime;

            // Rotate the character around world-y using x-axis of joystick
            thisTransform.Rotate(0.0f, camRotation.x, 0.0f, Space.World);

            // Rotate only the camera with y-axis input
            cameraPivot.Rotate(-camRotation.y, 0.0f, 0.0f);
        }
    }
Ejemplo n.º 9
0
 private void Start()
 {
     rotateJoystick = VCAnalogJoystickBase.GetInstance("RotateJoystic");
     moveJoystick = VCAnalogJoystickBase.GetInstance("MoveJoystic");
     if (rotateJoystick == null || moveJoystick == null)
     {
         Debug.LogError("UnitPlayer.Start() " + this.name + " 'rotateJoystick' or 'moveJoystick' component isn't assigned!!!");
         Debug.Break();
         return;
     }
 }
Ejemplo n.º 10
0
        public float UpdateMovement()
        {
            Vector3 inputVec = new Vector3();

            joy = VCAnalogJoystickBase.GetInstance("stick");

            float x = cameraController.transform.forward.x;
            float z = cameraController.transform.forward.z;

            float sum       = Mathf.Abs(x) + Mathf.Abs(z);
            float remainder = 1 - sum;

            float cameraX = (x + remainder * (x / sum) + 1) / 2;
            float cameraZ = (z + remainder * (z / sum) + 1) / 2;

            inputVec = new Vector3(Mathf.Lerp(-joy.AxisY, joy.AxisY, cameraX) + Mathf.Lerp(-joy.AxisX, joy.AxisX, cameraZ), 0,
                                   Mathf.Lerp(joy.AxisX, -joy.AxisX, cameraX) + Mathf.Lerp(-joy.AxisY, joy.AxisY, cameraZ));

            inputVec *= Speed;

            if ((inputVec.z != 0 || inputVec.x != 0))
            {
                if (!audioSource.isPlaying)
                {
                    audioSource.clip = moveSound;
                    audioSource.Play();
                }
            }
            else
            {
                GetComponent <AudioSource>().Stop();
            }

            controller.Move((inputVec + Vector3.up * -gravity + new Vector3(0, 0, 0)) * Time.deltaTime);

            if (freeRoam)
            {
                // Rotation
                if (inputVec != Vector3.zero)
                {
                    transform.rotation = Quaternion.Slerp(transform.rotation,
                                                          Quaternion.LookRotation(inputVec),
                                                          Time.deltaTime * rotationDamping);
                }
            }
            else if (currentFocus != null)
            {
                Quaternion lookRotation;
                Vector3    direction;

                direction    = (currentFocus.transform.position - transform.position).normalized;
                lookRotation = Quaternion.LookRotation(direction);

                transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationDamping / 3);
            }
            else
            {
                freeRoam = true;
            }

            return(inputVec.magnitude);
        }
Ejemplo n.º 11
0
    void FixedUpdate()
    {
        if (Network.peerType == NetworkPeerType.Server)
        {
            if (Input.GetAxis("Horizontal") > 0)
            {
                //transform.Translate (new Vector3 (speed*Time.deltaTime, 0, 0));
                Press(1, 0);
            }
            if (Input.GetAxis("Horizontal") < 0)
            {
                //transform.Translate (new Vector3 (-speed*Time.deltaTime, 0, 0));
                Press(-1, 0);
            }
            if (canClimb == true)
            {
                if (Input.GetAxis("Vertical") > 0)
                {
                    //transform.Translate (new Vector3 (0, speed*Time.deltaTime, 0));
                    Press(0, 1);
                }
                if (Input.GetAxis("Vertical") < 0)
                {
                    //transform.Translate (new Vector3 (0, -speed*Time.deltaTime, 0));
                    Press(0, -1);
                }
            }
        }
        if (Network.peerType == NetworkPeerType.Client)
        {
            VCAnalogJoystickBase moveJoystick = VCAnalogJoystickBase.GetInstance("MoveJoyStick");
            Vector2 directionVector           = new Vector2(moveJoystick.AxisX, moveJoystick.AxisY);
            if (directionVector != Vector2.zero)
            {
                // Get the length of the directon vector and then normalize it
                // Dividing by the length is cheaper than normalizing when we already have the length anyway
                var directionLength = directionVector.magnitude;
                directionVector = directionVector / directionLength;

                // Make sure the length is no bigger than 1
                directionLength = Mathf.Min(1.0f, directionLength);

                // Make the input vector more sensitive towards the extremes and less sensitive in the middle
                // This makes it easier to control slow speeds when using analog sticks
                directionLength = directionLength * directionLength;

                // Multiply the normalized direction vector by the modified length
                directionVector = directionVector * directionLength;

                if (Mathf.Abs(directionVector.x) > Mathf.Abs(directionVector.y) || canClimb == false)
                {
                    if (directionVector.x < 0)
                    {
                        GetComponent <NetworkView>().RPC("Press", RPCMode.Server, -1, 0);
                    }
                    if (directionVector.x > 0)
                    {
                        GetComponent <NetworkView>().RPC("Press", RPCMode.Server, 1, 0);
                    }
                }
                if (Mathf.Abs(directionVector.x) <= Mathf.Abs(directionVector.y) && canClimb == true)
                {
                    if (directionVector.y < 0)
                    {
                        GetComponent <NetworkView>().RPC("Press", RPCMode.Server, 0, -1);
                    }
                    if (directionVector.y > 0)
                    {
                        GetComponent <NetworkView>().RPC("Press", RPCMode.Server, 0, 1);
                    }
                }
            }

//			VCDPadBase dpad = VCDPadBase.GetInstance("dpad");
//			if (dpad){
//
//				if (dpad.Left)
//					networkView.RPC("Press", RPCMode.Server, -1, 0);
//				if (dpad.Right)
//					networkView.RPC("Press", RPCMode.Server, 1, 0);
//				if (canClimb){
//					if (dpad.Up)
//						networkView.RPC("Press", RPCMode.Server, 0, 1);
//					if (dpad.Down)
//						networkView.RPC("Press", RPCMode.Server, 0, -1);
//				}
//			}
        }
    }