简体   繁体   中英

Unity - Joystick Android camera spinning intermittent problem

I have an intermittent Camera/Movement problem I'm trying to solve that only occurs on an android mobile build, never within unity. I have a player object with a single joystick that controls movement and facing direction, the update loop below allows for the fact that if you spin your camera around we keep track of this angle and adjust the movement accordingly, this works perfectly.

Scattered around my world are various resources that you can collect triggered with an onmousedown to "pick them up" and destroys the gameobject once collected, this mechanism also works without fault. Where my problem lies is that if using the joystick to move the player close to a resource to collect it, then I use the onmousedown collect process show below I see sometimes some very erratic behavior.

The camera will begin to continue spin whilst using the joystick again (it stops if i release) and the movement is no longer obeyed properly (if I move scene and return everything is working again). This behavior only seems to happen if i am immediately next to the worldspace the resource is found, if the players position is slightly further away but still in range, the problem doesnt occur.

Because of the above, I thought it might be physics collisions screwing with the position of the player so i used the collision matrix and setup seperate layers so the resource couldnt affect the rigidbody but that doesnt seem to help. Could anybody point me in the right direction please?

PlayerMovement update loop -

private void Update() 
        {
            float xMovement = MoveJoystick.Horizontal();
            float zMovement = MoveJoystick.Vertical();  
            Vector3 inputDirection = new Vector3(xMovement, 0, zMovement);
            //Get the camera horizontal rotation
            Vector3 faceDirection = new Vector3(_cameraTransform.forward.x, 0, _cameraTransform.forward.z);
            //Get the angle between world forward and camera
            float cameraAngle = Vector3.SignedAngle(Vector3.forward, faceDirection, Vector3.up);
            //Finally rotate the input direction horizontally by the cameraAngle
            Vector3 moveDirection = Quaternion.Euler(0 , cameraAngle, 0) * inputDirection;
            _RB.velocity  = moveDirection * _speed;            
            if (_RB.velocity.magnitude > 0) 
            {
                GameConstants.ANIMATION_PLAYER_WALK = true;
                DebugLog(GameConstants.ANIMATION_PLAYER_WALK);
                if(moveDirection.sqrMagnitude > 0.1f)
                transform.LookAt(transform.position + moveDirection); 
                _lookdirection = moveDirection;
            }
            else
            {
                GameConstants.ANIMATION_PLAYER_WALK = false;
                DebugLog(GameConstants.ANIMATION_PLAYER_WALK);
                transform.rotation = Quaternion.LookRotation(_lookdirection);
            }                
            
        }

Resource collection -

 private void OnMouseDown()
        {
            _audioSource.PlayOneShot(_clickSound.clip);
            _objectPosition = transform.position;
            _playerPosition = GameConstants.PLAYER.transform;
            _distanceToObject = Vector3.Distance(_objectPosition, _playerPosition.position);

            if (_distanceToObject < _objectInteractDistance && GameConstants.MENU_ACTIVE == false)
            {
                _pointerDown = true;
                _pointerUp = false;

                GameConstants.UI_RESOURCE_PICKUP_BAR.SetActive(true);

                switch (_resourceType.ToString())
                {
                    case "Herb": GameConstants.UI_RESOURCE_PICKUP_SWEETSPOT_COMMON.SetActive(true);
                    break;

                    case "Wood": GameConstants.UI_RESOURCE_PICKUP_SWEETSPOT_UNCOMMON.SetActive(true);
                    break;

                DebugLog("Attempting to pickup resource");
            }
            else
            {
                DebugLog("Distance too far, cannot pickup! (" + _distanceToObject + ")");
                Invoke("ShowTooFarMessage", 0);
            }
        }

Resource Update loop controlling the collection -

 private void Update()
    {
        if (_pointerDown)
        {
            _pointerDownTimer += Time.deltaTime;

            if (_pointerDownTimer >= _requiredHoldTime)
            {
                if (OnLongClick != null)
                    OnLongClick.Invoke();

                Reset();
            }
            else
            {
                if (_pointerDownTimer > _sweetHoldTimeStart && _pointerDownTimer < _sweetHoldTimeEnd && _pointerUp)
                {
                    if (OnSweetClick != null)
                        OnSweetClick.Invoke();

                Reset();
                }
            }
            GameConstants.UI_RESOURCE_PICKUP_BAR_FILL.fillAmount = _pointerDownTimer / _requiredHoldTime;
        }
    }

It was a single null audio file triggering upon collection of all things. Unity can handle them, a mobile device cannot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM