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. Dismiss Notice

Animation script behaves differently in full screen mode

Discussion in 'Scripting' started by kpribbeck, Aug 23, 2020.

  1. kpribbeck

    kpribbeck

    Joined:
    Jun 24, 2018
    Posts:
    3
    Ok, so I'm creating my first FPS game and I stumbled upon some weird behaviour trying to animate a 3d object. As part of a reloading animation, I want my gun to eject it's current clip. So the clip is suposed to go flying in a parabole like movement while rotating.

    The problem is that in the editor, when I enter play mode, the animation behaves differently if I am in full screen mode or small screen. For some reason, when in full screen, the object flies around in a circle instead of a parabole. I assume it has something to do with my 'gravity' behaviour, since that is in charge of rotating the transform along the parabole. So in full screen mode 'gravity' is applied much faster than in small screen.

    I have the following script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponClipEject : MonoBehaviour
    6. {
    7.     [Header("Reference to parent transform")]
    8.     public Transform parent;
    9.  
    10.     [Header("Animation parameters")]
    11.     [Tooltip("Gravity force that will pull the gameobject down")]
    12.     public float gravity = 6;
    13.     [Tooltip("Force applied to eject the gameobject")]
    14.     public float ejectForce = 3.5f;
    15.     [Tooltip("Delay bewteen the eject action is called and the clip is ejected. If zero, the action is instant")]
    16.     public float ejectDelay = 0.1f;
    17.     [Tooltip("Speed at which the gameobject will be rotating")]
    18.     public float rotationSpeed = 3;
    19.     [Tooltip("Delay between ejection and the start of the rotating animation")]
    20.     public float rotationDelay;
    21.     [Tooltip("Lifetime of the eject animation before gameobject is disabled")]
    22.     public float effectDuration = 2f;
    23.  
    24.     float _ejectTime;
    25.     Vector3 _movementVector;
    26.     // transform to apply rotation animation.
    27.     Transform rotationTransform;
    28.  
    29.     void Start()
    30.     {
    31.         rotationTransform = transform.GetChild(0);
    32.     }
    33.  
    34.     public void Eject()
    35.     {
    36.         StartCoroutine(EjectRoutine());
    37.     }
    38.  
    39.     public IEnumerator EjectRoutine()
    40.     {
    41.         _ejectTime = Time.time;
    42.         _movementVector = -Vector3.forward * ejectForce;
    43.  
    44.         while (Time.time - _ejectTime < effectDuration)
    45.         {
    46.             // Apply gravity in form of transform rotation.
    47.             // In this case, to animate the ejected clip, we translate the clip's transform along its negative z axis,
    48.             // so by rotation the transform we achieve a gravity-like effect
    49.             Debug.Log(_movementVector);
    50.             transform.Rotate(-transform.InverseTransformDirection(transform.right), gravity);
    51.             transform.Translate(_movementVector * Time.deltaTime);
    52.  
    53.             // To apply a rotation effect we rotate a child transform the contains the 3D model of the clip
    54.             // this way we achieve the desired animation by manipulating two transforms (parent and child)
    55.             rotationTransform.Rotate(transform.InverseTransformDirection(transform.forward), rotationSpeed);
    56.             yield return null;
    57.         }
    58.     }
    59. }
    60.  
    And this is my gameobject heirarchy:
    • Weapon
    • -> WeaponClip
    • -> -> RotationTransform
    • ->->-> 3D Mesh of the clip
    Unity version: 2019.4.4
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Have you considered just deparenting the flying object, then you can spin it however you like for a moment until it hits the ground?
     
  3. kpribbeck

    kpribbeck

    Joined:
    Jun 24, 2018
    Posts:
    3
    I probably will end up doing something like that, but still, i would like to understand why the scripts behaves differently when I try it in full screen mode and small screen.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You are rotating without regard to framer duration (Time.deltaTime).

    You are moving with regard to Time.deltaTime, as you should be.

    Is that your problem?

    Also, why are you rotating for a trajectory? If you review your basic dynamics equations, you want to add the gravity each frame to the velocity downwards. That will be be a proper trajectory, not an orbit.

    I like to keep stuff simple and broken apart. Here's my typical ballistic script for motion:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ballistic : MonoBehaviour
    5. {
    6.     public Vector3 velocity;
    7.     public Vector3 gravity;
    8.  
    9.     void Update ()
    10.     {
    11.         velocity += gravity * Time.deltaTime;
    12.         transform.position += velocity * Time.deltaTime;
    13.     }
    14. }

    This is my usual "die off in X seconds" script, which I call TTL, which stands for "time to live:"

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class TTL : MonoBehaviour
    4. {
    5.     public float AgeLimit;
    6.     float age;
    7.  
    8.     void Update ()
    9.     {
    10.         if (age > AgeLimit)
    11.         {
    12.             Destroy(gameObject);
    13.             return;
    14.         }
    15.  
    16.         age += Time.deltaTime;
    17.     }
    18. }
    And here's how I make stuff spin:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SpinRotate : MonoBehaviour
    4. {
    5.     public Vector3 rotation;
    6.  
    7.     void Update ()
    8.     {
    9.         transform.Rotate( rotation * Time.deltaTime);
    10.     }
    11. }
    Pretty much write little scripts like this once, use them again and again, just configured differently.
     
    kpribbeck likes this.
  5. kpribbeck

    kpribbeck

    Joined:
    Jun 24, 2018
    Posts:
    3

    Thanks a lot for your answer and for taking the time to provide an alternate solution, I didn't realize I wasn't applying deltaTime to my rotation, that fixes the issue I was complicating myself with. I appreciate your solution for describing the trayectory, It's much simpler and easy to read than what I had.
     
    Kurt-Dekker likes this.