示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (ParentController != null || singlePlayer)
        {
            float leftStickX = 0f;
            float leftStickY = 0f;

            if (UseMouse)
            {
                leftStickX = Input.GetAxis("Horizontal");
                leftStickY = Input.GetAxis("Vertical");
            }
            else
            {
                leftStickX = Input.GetAxis("cHorizontal");
                leftStickY = Input.GetAxis("cVertical");
            }

            float rightStickX = Input.GetAxis("AimHorizontal");
            float rightStickY = Input.GetAxis("AimVertical");

            float controllerAim = Input.GetAxis("FireXbox");

            if (!isRunning && (leftStickX != 0.0f || leftStickY != 0.0f))
            {
                if (_animator != null)
                {
                    _animator.SetTrigger("Run");
                    isRunning = true;
                    // Debug.Log("Run");
                }
            }
            else if (isRunning && (leftStickX == 0.0f && leftStickY == 0.0f))
            {
                if (_animator != null)
                {
                    _animator.SetTrigger("Idle");
                }
                isRunning = false;
                //  Debug.Log("Idle");
            }

            var targetPos = /*transform.position +*/ new Vector3(leftStickX * Time.deltaTime * Speed,
                                                                 0f,
                                                                 leftStickY * Time.deltaTime * Speed);

            if (singlePlayer)
            {
                transform.Translate(leftStickX * Time.deltaTime * Speed,
                                    0f,
                                    leftStickY * Time.deltaTime * Speed,
                                    Space.World);
            }

            //Debug.Log("Translate: "+transform.position.ToString());
            //Debug.Log(targetPos.ToString());

            Quaternion targetRotation = new Quaternion();

            if (UseMouse)
            {
                //var lookPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
                //lookPos.y = 0;
                //var rotation = Quaternion.LookRotation(lookPos);
                //transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
                //Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));

                // Generate a plane that intersects the transform's position with an upwards normal.
                Plane playerPlane = new Plane(Vector3.up, transform.position);

                // Generate a ray from the cursor position
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                // Determine the point where the cursor ray intersects the plane.
                // This will be the point that the object must look towards to be looking at the mouse.
                // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
                //   then find the point along that ray that meets that distance.  This will be the point
                //   to look at.
                float hitdist = 0.0f;
                // If the ray is parallel to the plane, Raycast will return false.
                if (playerPlane.Raycast(ray, out hitdist))
                {
                    // Get the point along the ray that hits the calculated distance.
                    Vector3 targetPoint = ray.GetPoint(hitdist);

                    // Determine the target rotation.  This is the rotation if the transform looks at the target point.
                    var lookRotation = Quaternion.LookRotation(targetPoint - transform.position);

                    // Smoothly rotate towards the target point.
                    targetRotation = Quaternion.Slerp(transform.rotation, lookRotation, rotationSpeed);                    //Damping * Time.deltaTime);

                    //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed);//Damping * Time.deltaTime);
                    if (singlePlayer)
                    {
                        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotationSpeed);                        //Damping * Time.deltaTime);
                    }
                }
            }
            else
            {
                float angle = Mathf.Atan2(rightStickX, -rightStickY) * Mathf.Rad2Deg;

                targetRotation = Quaternion.Euler(0f, angle, 0.0f);

                //if (rightStickX != 0 || rightStickY != 0)
                if (singlePlayer)
                {
                    transform.rotation = Quaternion.Euler(0f, angle, 0.0f);
                }
            }

            if (UseMouse)
            {
                if (isAiming)
                {
                    aimTime += Time.deltaTime;
                }

                if (Input.GetMouseButtonDown(0) && !isAiming)
                {
                    isAiming = true;
                    _animator?.SetBool("Aim", true);
                }

                if (Input.GetMouseButtonUp(0) && isAiming)
                {
                    isAiming = false;
                    _animator?.SetBool("Aim", false);
                    if (aimTime > 0f && (Ammo > 0 || Ammo == -1))
                    {
                        Vector3 spawnPos = transform.position + transform.forward * bulletSpawnDistance;
                        Vector3 Vector   = transform.forward * Mathf.Clamp(aimTime, .01f, 1f);
                        Vector.y = 0;
                        //Debug.Log("Spawn: " + spawnPos);
                        //Debug.Log("distance: " + bulletSpawnDistance);
                        //Debug.Log("forward: " + transform.forward);
                        ParentController?.SpawnBullet(spawnPos, Vector, targetRotation.eulerAngles);

                        Ammo--;
                        aimTime = 0f;
                    }
                }
            }
            else
            {
                //controller aiming
                if (isAiming)
                {
                    aimTime += Time.deltaTime;
                }

                if (controllerAim > 0 && !isAiming)
                {
                    isAiming = true;
                    _animator?.SetBool("Aim", true);
                }

                if (controllerAim == 0 && isAiming)
                {
                    isAiming = false;
                    _animator?.SetBool("Aim", false);
                    if (aimTime > 0f && (Ammo > 0 || Ammo == -1))
                    {
                        Vector3 spawnPos = transform.position + transform.forward * bulletSpawnDistance;
                        Vector3 Vector   = transform.forward * Mathf.Clamp(aimTime, .01f, 1f);
                        Vector.y = 0;
                        //Debug.Log("Spawn: " + spawnPos);
                        //Debug.Log("distance: " + bulletSpawnDistance);
                        //Debug.Log("forward: " + transform.forward);
                        ParentController?.SpawnBullet(spawnPos, Vector, targetRotation.eulerAngles);

                        Ammo--;
                        aimTime = 0f;
                    }
                }
            }

            if (!singlePlayer)
            {
                absoluteThresholdCount++;
                if (absoluteThresholdCount > updateAbsoluteThreshold)
                {
                    absoluteThresholdCount = 0;
                    //transform.position += targetPos;
                    //this.transform.rotation = Quaternion.Euler(targetRotation.eulerAngles);
                    ParentController.AbsoluteUpdatePlayer(transform.position + targetPos, targetRotation.eulerAngles);
                }
                else
                {
                    ParentController.UpdateMyPlayer(targetPos, targetRotation.eulerAngles);
                }
            }
        }
    }