Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Fullscreen mode is altering my Player script somehow

Discussion in 'Editor & General Support' started by ManofDog, Jul 17, 2020.

  1. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    My character has functionality that lets it turn around 360 degrees. When I have the Game view window minimized I get normal behavior, but when I enter Fullscreen mode the character spins a lot faster than it did.

    Is there something I'm missing here?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Something in your code is probably screen-resolution dependent.
     
  3. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    How can I tell? I have my movement code under private void CalculateMovement(); and it gets called under private void Update()
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Maybe if you share your code someone here on the forums will notice something wrong with it?
     
  5. ManofDog

    ManofDog

    Joined:
    Nov 6, 2019
    Posts:
    9
    using System.Collections;
    using TMPro;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    [SerializeField] private float _moveSpeed = 10f;
    [SerializeField] private float _turnSpeed = 5f;
    [SerializeField] private float _sprintSpeed = 4f;

    public float horizontal;
    public float vertical;

    CharacterController _characterController;
    public bool isGrounded;

    Quaternion _targetRotation;
    Coroutine _rotation;
    public float rotationSpeed = 360.0f;


    private void Awake()
    {
    _characterController = GetComponent<CharacterController>();
    }

    private void Update()
    {
    CalculateMovement();

    if (Input.GetKey(KeyCode.LeftShift) && vertical > 0)
    {
    if (_rotation != null)
    {
    StopCoroutine(_rotation);
    _rotation = null;
    }
    Sprint();
    }

    else if (Input.GetKeyDown(KeyCode.Tab))
    {
    _targetRotation = Quaternion.Euler(0, 180, 0) * transform.rotation;

    if(_rotation == null)
    {
    _rotation = StartCoroutine(RotateToTargetOvertTime());
    }
    }
    }

    private void CalculateMovement()
    {
    vertical = Input.GetAxis("Vertical");
    horizontal = Input.GetAxis("Horizontal");

    Vector3 direction = new Vector3(0f, 0f, vertical);

    Vector3 movement = transform.TransformDirection(direction) * _moveSpeed;

    transform.Rotate(0f, horizontal * _turnSpeed, 0f);

    isGrounded = _characterController.SimpleMove(movement);
    }

    private void Sprint()
    {
    float newTurnSpeed = _turnSpeed - 1.4f; //this reduces the speed at which the player turns while sprinting, so they don't turn on a dime
    //3 or 3.5 should be a good newTurnSpeed

    vertical = Input.GetAxis("Vertical");
    horizontal = Input.GetAxis("Horizontal");

    Vector3 direction = new Vector3(0f, 0f, vertical);
    Vector3 movement = transform.TransformDirection(direction) * _sprintSpeed;

    transform.Rotate(0f, horizontal * newTurnSpeed, 0f); //add the new turn speed

    isGrounded = _characterController.SimpleMove(movement);
    }

    IEnumerator RotateToTargetOvertTime()
    {
    while(Quaternion.Angle(transform.rotation, _targetRotation) > 1f) //in this case, 1f means degrees
    {
    transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, rotationSpeed * Time.deltaTime);

    yield return null;
    }
    _rotation = null;
    }

    }