//------------------------------------------------------------------

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        script = target as MouseSoftware.EasyTerrain;
        if (!Application.isPlaying)
        {
            script.Initialize();
            Menu();
        }
        else
        {
            EditorGUILayout.Space();
            EditorGUILayout.HelpBox("Exit PlayMode to configure settings", MessageType.Warning, true);
            EditorGUILayout.Space();
        }
    }
Beispiel #2
0
        //================================================================================

        //private void OnCollisionEnter(Collision collision)
        //{
        //    Debug.Log("Time: " + Time.time + "   collided with: " + collision.collider.name);
        //}

        //================================================================================

        private void FixedUpdate()
        {
            deltaTime    = Time.fixedDeltaTime;
            smoothFactor = (smoothingTime > deltaTime) ? deltaTime / smoothingTime : 1f;

            inputMouse = Vector3.zero;
            inputWASD  = Vector3.zero;

            // mouse input (desktop / laptop)
            if (SystemInfo.deviceType == DeviceType.Desktop)
            {
                inputMouse  = (SystemInfo.deviceType != DeviceType.Handheld) ? new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0f) : Vector3.zero;
                inputMouse *= deltaTime * sensitivity * 5f;
                if (invertMouseY)
                {
                    inputMouse.x *= -1;
                }
                inputWASD = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
                speedMultiplierEnabled = (Input.GetKey(KeyCode.LeftShift)) ? true : false;
            }

            // touch input (mobile devices)
            if (SystemInfo.deviceType == DeviceType.Handheld)
            {
                foreach (Touch touch in Input.touches)
                {
                    if (touch.fingerId == inputTouchRotateID)
                    {
                        if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
                        {
                            inputTouchRotateID = -1;
                            continue;
                        }
                    }
                    if (touch.fingerId == inpuTouchMoveID)
                    {
                        if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
                        {
                            inpuTouchMoveID        = -1;
                            speedMultiplierEnabled = false;
                            continue;
                        }
                    }
                    if (touch.phase == TouchPhase.Began)
                    {
                        if (touch.position.x > Screen.width * 0.5)
                        {
                            inputTouchRotateID = touch.fingerId;
                            continue;
                        }
                        if (touch.position.x < Screen.width * 0.5)
                        {
                            inpuTouchMoveID        = touch.fingerId;
                            inputTouchMoveCenter   = touch.position;
                            speedMultiplierEnabled = (touch.tapCount > 1) ? true : false;
                            continue;
                        }
                        continue;
                    }
                    if (touch.fingerId == inputTouchRotateID)
                    {
                        if (touch.phase == TouchPhase.Moved)
                        {
                            inputMouse  = new Vector3(-touch.deltaPosition.y, touch.deltaPosition.x, 0f);
                            inputMouse *= deltaTime * sensitivity * 0.2f;
                        }
                    }
                    if (touch.fingerId == inpuTouchMoveID)
                    {
                        inputWASD  = new Vector3(touch.position.x - inputTouchMoveCenter.x, 0f, touch.position.y - inputTouchMoveCenter.y);
                        inputWASD /= Screen.height;
                        inputWASD *= 10f;
                        if (inputWASD.sqrMagnitude > 1f)
                        {
                            inputWASD.Normalize();
                        }
                    }
                }
            }

            // rotation
            heading            = Mathf.MoveTowardsAngle(heading, heading + inputMouse.y, sensitivity);
            pitch              = Mathf.MoveTowardsAngle(pitch, pitch + inputMouse.x, sensitivity);
            pitch              = Mathf.Clamp(pitch, pitchMin, pitchMax);
            roll               = 0f;
            newRotation        = Quaternion.Euler(0f, heading, 0f) * Quaternion.Euler(pitch, 0f, 0f) * Quaternion.Euler(0f, 0f, roll);
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, smoothFactor);
            rb.angularVelocity = Vector3.zero;

            // translation
            float speedMultiplierTemp = speedMultiplierEnabled ? speedMultiplier : 1f;

            localVelocity  = transform.InverseTransformDirection(rb.velocity);
            localVelocity += inputWASD * acceleration * deltaTime * speedMultiplierTemp;
            rb.velocity    = transform.TransformDirection(localVelocity);

            terrainSampleAtPlayer = EasyTerrain.GetTerrainSample(transform.position);
            if (transform.position.y < terrainSampleAtPlayer.height + minHeightToGround)
            {
                transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, terrainSampleAtPlayer.height + minHeightToGround, maxHeight), transform.position.z);
                rb.velocity        = Vector3.RotateTowards(rb.velocity, Vector3.ProjectOnPlane(rb.velocity, terrainSampleAtPlayer.normal) * rb.velocity.magnitude, Mathf.PI * 0.5f * deltaTime, 1f * deltaTime);
            }
            else
            {
                rb.velocity = Vector3.RotateTowards(rb.velocity, transform.TransformDirection(inputWASD) * rb.velocity.magnitude, Mathf.PI * 0.5f * deltaTime, 1f * deltaTime);
            }

            if (inputWASD.sqrMagnitude > 0.01f)
            {
                rb.drag = 0.0f;
            }
            else
            {
                rb.drag      = 0.75f;
                rb.velocity *= 0.5f;
            }

            if (rb.velocity.magnitude > maxSpeed * speedMultiplierTemp)
            {
                speed       = Mathf.Lerp(rb.velocity.magnitude, maxSpeed * speedMultiplierTemp, smoothFactor);
                rb.velocity = rb.velocity.normalized * speed;
            }
        }