Search Unity

Script broken after Unity upgrade

Discussion in 'Scripting' started by Yannick_Stoot, Nov 22, 2017.

  1. Yannick_Stoot

    Yannick_Stoot

    Joined:
    Sep 17, 2014
    Posts:
    69
    Hey guys!

    I've recently upgraded my project to Unity 2017.2.0f3 and the project which works in previous versions does not with the newest one. The code is listed below and I've narrowed the problem down to the "AddRelativeForce" part. I can confirm that the object rotates to where is clicked but the direction of the force is somewhat equal to the way AddForce works. It ignores the rotation the object that is being shot is in.

    Can anyone help me out?

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ScreenShooter : MonoBehaviour
    7. {
    8.     //    Value used to store the force by which the object is shot
    9.     public int force = 100;
    10.     //  
    11.     public int timer = 5;
    12.     //    Value used to store all possible primitives that can be shot
    13.     public enum Selectibles { Sphere, Capsule, Cube, Cylinder, Custom };
    14.     //    Value used to select the GameOobject that will to be shot
    15.     public Selectibles selectedObject = Selectibles.Custom;
    16.     //    Variable used to store the custom GameObject
    17.     [HideInInspector]
    18.     public GameObject customObject;
    19.     [HideInInspector]
    20.     public GameObject projectile;
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetMouseButtonDown(0))
    25.         {
    26.             //    Check if the mouse is not on an UI object
    27.             if (!EventSystem.current.IsPointerOverGameObject())
    28.             {
    29.                 GameObject bullet = CreateProjectile();
    30.                 Camera mainCamera = Camera.main;
    31.  
    32.                 //    Position the bullet at the camera's transform
    33.                 bullet.transform.position = mainCamera.transform.position;
    34.  
    35.  
    36.                 Rigidbody rigidbody = new Rigidbody();
    37.                 if ((selectedObject != Selectibles.Custom) || (bullet.GetComponent<Rigidbody>() == null))
    38.                 {
    39.                     rigidbody = bullet.AddComponent<Rigidbody>();
    40.                     rigidbody.mass = 0.5f;
    41.                     rigidbody.angularDrag = 0.02f;
    42.                     rigidbody.drag = 0.02f;
    43.                     rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
    44.                     bullet.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
    45.                 }
    46.                 else
    47.                 {
    48.                     rigidbody = bullet.GetComponent<Rigidbody>();
    49.                 }
    50.  
    51.                 //    Add force to the bullet in the direction to where the user clicks
    52.                 Vector3 targetDirection = new Vector3(Input.mousePosition.x, Input.mousePosition.y, mainCamera.nearClipPlane);
    53.  
    54.                 bullet.transform.LookAt(mainCamera.ScreenToWorldPoint(targetDirection));
    55.                 rigidbody.AddRelativeForce(Vector3.forward * force);
    56.                
    57.                 //    Set a timer which will destroy the object
    58.                 Destroy(bullet, timer);
    59.             }
    60.         }
    61.     }
    62.  
    63.     //    Function used to assign the right primitive
    64.     private GameObject CreateProjectile()
    65.     {
    66.         //
    67.         GameObject returnObject;
    68.  
    69.         //
    70.         switch (selectedObject)
    71.         {
    72.             case Selectibles.Sphere:
    73.                 returnObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    74.                 return returnObject;
    75.  
    76.             case Selectibles.Capsule:
    77.                 returnObject = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    78.                 return returnObject;
    79.  
    80.             case Selectibles.Cube:
    81.                 returnObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    82.                 return returnObject;
    83.  
    84.             case Selectibles.Cylinder:
    85.                 returnObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    86.                 return returnObject;
    87.  
    88.             case Selectibles.Custom:
    89.                 returnObject = Instantiate(customObject) as GameObject;
    90.                 return returnObject;
    91.  
    92.             default:
    93.                 return null;
    94.         }
    95.     }
    96. }
    97.  
    98. [CustomEditor(typeof(ScreenShooter))]
    99. public class CE_ScreenShooter : Editor
    100. {
    101.     public override void OnInspectorGUI()
    102.     {
    103.         base.OnInspectorGUI();
    104.  
    105.         //    Store the ScreenShooter class
    106.         var screenShooterClass = target as ScreenShooter;
    107.  
    108.         //
    109.         if (screenShooterClass.selectedObject == ScreenShooter.Selectibles.Custom)
    110.         {
    111.             screenShooterClass.customObject = (GameObject)EditorGUILayout.ObjectField(screenShooterClass.customObject, typeof(GameObject), false);
    112.         }
    113.     }
    114. }
    115.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Adding forces to rigidbodies is supposed to be done in FixedUpdate, rather than in Update. I'm not sure that will fix your issue, but since you're doing that incorrectly that would be the first thing I'd try.
     
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
  4. Yannick_Stoot

    Yannick_Stoot

    Joined:
    Sep 17, 2014
    Posts:
    69
    @BlackPete Wait, this is a bug? Was not expecting that! Will look into the solution. Thanks for replying!

    @Joe-Censored Why should this be done in the fixed update rather than the normal update?

    Edit:
    Fixed it through the solution provided by BlackPete. The code I needed to add was
    Code (CSharp):
    1. Physics.SyncTransforms
    It should be added after the LookAt like this:
    Code (CSharp):
    1. //    Add force to the bullet in the direction to where the user clicks
    2. Vector3 targetDirection = new Vector3(Input.mousePosition.x, Input.mousePosition.y, mainCamera.nearClipPlane);
    3.  
    4. bullet.transform.LookAt(mainCamera.ScreenToWorldPoint(targetDirection));
    5. Physics.SyncTransforms();
    6. rigidbody.AddRelativeForce(Vector3.forward * force);
     
    Last edited: Nov 22, 2017
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I only know what they tell me :p

    https://docs.unity3d.com/ScriptReference/Rigidbody.html

     
  6. Yannick_Stoot

    Yannick_Stoot

    Joined:
    Sep 17, 2014
    Posts:
    69
    Hahha. Well that clears up a bit. In this context I do need to use Update though since in is used for input. Using the FixedUpdate would make me miss some input. Plus I only apply force ones so it is not a repeated practice, one in which I would need a FixedUpdate. Thanks for sharing though!