Upgrade to Pro — share decks privately, control downloads, hide ads and more …

【Unity道場京都スペシャル】プロが教える脱初心者スクリプト術!

unitydojo
November 05, 2016
2.6k

 【Unity道場京都スペシャル】プロが教える脱初心者スクリプト術!

unitydojo

November 05, 2016
Tweet

More Decks by unitydojo

Transcript

  1. ूதߨ࠲ͦͷ3

    ϓϩ͕ڭ͑Δ୤ॳ৺ऀεΫϦϓτज़ʂ
    ϢχςΟɾςΫϊϩδʔζɾδϟύϯ
    ϑΟʔϧυɾΤϯδχΞ ҆ݪ ༞ೋ

    View full-size slide

  2. ෺ཧΛ࢖ͬͨ
    ूதߨ࠲ͦͷ3

    ϓϩ͕ڭ͑Δ୤ॳ৺ऀεΫϦϓτज़ʂ
    ϢχςΟɾςΫϊϩδʔζɾδϟύϯ
    ϑΟʔϧυɾΤϯδχΞ ҆ݪ ༞ೋ

    View full-size slide

  3. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ຊ೔ͷ͓͸ͳ͠
    •ಈ͖Λ࡞ΔςΫχοΫ
    • ϓϥϯφʔɺΞʔςΟετ͞Μʹ΋໾ʹ
    ཱͭ΋ͷ

    View full-size slide

  4. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ຊ೔ͷ͓͸ͳ͠
    •࣮ԋ͋Γ
    •࣮ԋ಺༰΋ࢿྉʹೖΕͯ͋Δ
    •ϝϞΛऔΔඞཁ͸ͳ͠ʂ

    View full-size slide

  5. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ຊ೔ͷ͓͸ͳ͠
    •঺հ͢ΔϓϩάϥϜ͸ͱͯ΋γϯϓϧ
    • ॲཧΛྻڍ͢Δ͚ͩ
    • if จ͢Βॻ͔ͳ͍

    View full-size slide

  6. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ຊ೔ͷ͓͸ͳ͠
    •ʮ͋ͱͰ΍ͬͯΈΑ͏ʂʯ
    •ͱࢥͬͯ΋Β͑ΔΑ͏ʹ͕Μ͹Γ·͢ʂ

    View full-size slide

  7. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    ·ͣ͸γϯϓϧʹ
    ΩϟϥΫλʔΛ
    ಈ͔ͯ͠ΈΑ͏

    View full-size slide

  8. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  9. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  10. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  11. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  12. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    InputManager ͰೖྗΛγϯϓϧ
    ʹ͓ͯ͜͠͏

    View full-size slide

  13. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  14. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Time.deltaTime Λ࢖͓͏

    View full-size slide

  15. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtʹ͍ͭͯ෮श

    View full-size slide

  16. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ήʔϜ͕ಈͬͯ͘Ͳ͏͍͏͜ͱʁ
    ࣌ؒ
    ࡉ͔͍ॲཧΛ܁Γฦͯ͠ಈ͖Λ࡞͍ͬͯΔ
    ॲཧʢUpdateʣ
    ɾɾɾɾɾɾɾɾɾɾɾ

    View full-size slide

  17. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtͱ͸
    ࣌ؒ
    ͻͱͭͷॲཧʹ͔͚Δ͕࣌ؒΔt
    Δt

    View full-size slide

  18. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtͱ͸
    ࣌ؒ
    ͻͱͭͷॲཧΛ̍ϑϨʔϜͱݺͿ
    Δt

    View full-size slide

  19. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtͱ͸
    ඵؒ20ϑϨʔϜͷ৔߹
    Δt
    1ඵ
    20ϑϨʔϜ

    View full-size slide

  20. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtͱ͸
    Δtͷฏۉ͸1/20ඵ
    Δt
    1ඵ
    20ϑϨʔϜ

    View full-size slide

  21. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Δtͱ͸
    ͭ·ΓɺΔt͸ඵؒϑϨʔϜ਺ͷٯ਺
    Δt
    1ඵ
    20ϑϨʔϜ

    View full-size slide

  22. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    Δt ΛϓϩάϥϜʹ૊Έࠐ΋͏

    View full-size slide

  23. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    FixedUpdate Λ࢖͓͏

    View full-size slide

  24. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  25. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ʲ֮ॻʳ
    Update ͸̍ඵؒʹԿճݺ͹ΕΔ͔Θ͔Βͳ͍ʢੑೳ͕௿͍؀ڥͩͱ
    গͳ͘ͳΔʣ
    →FixedUpdate ͸̍ඵؒʹݺ͹ΕΔճ਺͕ݻఆ
    Time.deltaTime ͸มಈ͢Δʢಉ͡ϑϨʔϜ಺Ͱ͸ݻఆʣ
    →Time.fixedDeltaTime ͸஋͕ݻఆ
    FixedUpdate Λ࢖͏ͱγϯϓϧʹʂ Δt ͸ Time.fixedDeltaTime

    View full-size slide

  26. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    Rigidbody Λ࢖͓͏

    View full-size slide

  27. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  28. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  29. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    AddForce ʹೖྗ஋Λ
    ͦͷ··ೖΕΔ

    View full-size slide

  30. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  31. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    Drag Λ࢖ͬͯ෺ମΛࢭΊΑ͏

    View full-size slide

  32. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  33. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Drag Λ੍͢Δ΋ͷ͸
    Rigidbody Λ੍͢Δ

    View full-size slide

  34. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Ұఆͷ֎ྗΛՃ͑Δͱɺ͋Δ଎౓ʹऩଋ͢Δ
    v
    t

    View full-size slide

  35. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࠷ऴతͳ଎౓ɺͦΕ͕ऴ୺଎౓
    v
    t

    View full-size slide

  36. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ऴ୺଎౓Λࢉग़͢Δهࣄ͕͋Γ·ͨ͠ʂ

    View full-size slide

  37. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    http://qiita.com/yuji_yasuhara/
    items/1f438f0f27f5ef854a73

    View full-size slide

  38. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ྗɺ࣭ྔɺυϥοά͕༩͑ΒΕͨͱ͖ͷऴ୺଎౓ v’
    m:࣭ྔ
    k:υϥοά
    F0:֎ྗ
    v’:ऴ୺଎౓

    View full-size slide

  39. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ऴ୺଎౓ v’ ΛಘΔͨΊͷ֎ྗʢ୯ͳΔࣜͷมܗʣ
    m:࣭ྔ
    k:υϥοά
    F0:֎ྗ
    v’:ऴ୺଎౓

    View full-size slide

  40. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  41. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    v
    t

    View full-size slide

  42. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    όωΛѻ͓͏

    View full-size slide

  43. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    όωͱ͸ɺڑ཭ʹൺྫ͢Δྗͷ͜ͱ
    ऑ͍
    ڧ͍

    View full-size slide

  44. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    όωΛϓϩάϥϜͰॻ͘
    void AddSpringForce(Vector3 target_position, float r)
    {
    var diff = target_position - transform.position;
    var force = diff * r;
    rigidbody_.AddForce(force);
    }

    View full-size slide

  45. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Φʔόʔγϡʔτʢ໨ඪϙΠϯτΛ௨Γա͗Δʣ
    x
    t

    View full-size slide

  46. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Φʔόʔγϡʔτ͠ͳ͍όω܎਺
    Λ஌Γͨ͘ͳΔ

    View full-size slide

  47. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Φʔόʔγϡʔτ͠ͳ͍৚݅
    m:࣭ྔ
    k:υϥοά
    r:όω܎਺

    View full-size slide

  48. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ʲ֮ॻʳ

    View full-size slide

  49. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ճసΛ࢖͓͏

    View full-size slide

  50. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    AddTorque Λཧղ͠Α͏

    View full-size slide

  51. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    τϧΫ͸෺ମΛճసͤ͞Δྗ

    View full-size slide

  52. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    AddForce ͸Ґஔ
    AddTorque ͸ճస

    View full-size slide

  53. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    τϧΫ͸
    ճస࣠ͱେ͖͞Ͱද͢

    View full-size slide

  54. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    AddTorque(new Vector3(2, 0, 0))
    ͸
    X࣠ʹରͯ͠2ͷτϧΫ

    View full-size slide

  55. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    τϧΫͷ͍͢͝ͱ͜Ζ
    ॏͶͯʢ଍ͯ͠ʣ΋େৎ෉ʂ

    View full-size slide

  56. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    ඈߦػͷૢ࡞Λ࡞ͬͯΈΑ͏

    View full-size slide

  57. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  58. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    Input.GetAxis Ͱ AddTorque

    View full-size slide

  59. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  60. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    Ϥʔ(yaw)
    ϐον(pitch)
    ϩʔϧ(roll)

    View full-size slide

  61. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ϩʔϧͯ͠͠·͏໰୊

    View full-size slide

  62. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ੵΛ࢖͓͏

    View full-size slide

  63. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ੵͱ͸ɾɾɾ
    •֎ੵͷ݁Ռ͸ϕΫτϧʂ
    •AxB ͱදه͢Δʢ͜ͱ͕ଟ͍ʣ
    •x͔ͩΒΫϩεੵͱ΋ݺͿɻӳޠͰݴ͏ͱ cross product
    •Unity Ͱ͸ Vector3.Cross ͱ͍͏ؔ਺
    •var C = Vector3.Cross(A, B); ͱॻ͘ͱɺ ϕΫτϧC͸AxBʹͳΔ
    •ϕΫτϧ A:(Ax, Ay, Az) ͱ B:(Bx, By, Bz) ͷ֎ੵΛ C:(Cx, Cy, Cz) ͱ͢Δͱ
    Cx = AyBz - AzBy
    Cy = AzBx - AxBz
    Cz = AxBy - AyBx ͳʹ΍Βਆൿతɾɾɾʂɹɹɹɹɹɹɹɹɹɹɹ

    View full-size slide

  64. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ੵAxB ʢ AͱBͷ໘ʹਨ௚ɺ௕͕͞ฏߦ࢛ลܗͷ໘ੵʣ
    B
    A
    AxB
    |A||B|sinθ
    θ

    View full-size slide

  65. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ݱࡏͷ޲͖͕Aͩͱͯ͠ɺBͷํ޲ʹ޲͚͍ͨͱ͖ͷτϧΫ͸ʁ
    B
    A

    View full-size slide

  66. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    τϧΫ͸ճస࣠ɺͭ·Γ֎ੵͷํ޲ʂ
    B
    A

    View full-size slide

  67. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ΄Μͷগ͔͕ࠩ͠͠ͳ͍ͱ͖͸τϧΫΛখ͍ͨ͘͞͠
    B
    A

    View full-size slide

  68. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ੵͦͷ·Μ·ͰɺΘΓͱόωͬΆ͘ͳΔʂ
    A
    AxB
    |A||B|sinθ
    B

    View full-size slide

  69. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ֎ੵͦͷ·Μ·ͰɺΘΓͱόωͬΆ͘ͳΔʂ
    A
    AxB
    |A||B|sinθ
    B
    90°Ҏ্Ͱ΋όω͕ऑ͘ͳΔͷͰ஫ҙ

    ʢAtan Λར༻͢ΔͳͲͰճආՄೳʣ

    View full-size slide

  70. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  71. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  72. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  73. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  74. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    ศརؔ਺

    AddRelativeTorque

    View full-size slide

  75. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  76. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    ඈߦػͳͷͰ
    Ϥʔ͸ϩʔϧ͠ͳ͕Β

    View full-size slide

  77. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ࣮ԋύʔτ
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  78. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    લਐʂ

    View full-size slide

  79. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ऴ୺଎౓ v’ ΛಘΔͨΊͷ֎ྗʢ࠶ܝʣ
    m:࣭ྔ
    k:υϥοά
    F0:֎ྗ
    v’:ऴ୺଎౓

    View full-size slide

  80. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  81. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN

    View full-size slide

  82. COPYRIGHT 2016 @ UNITY TECHNOLOGIES JAPAN
    ͓͠·͍
    ͜ͷߨԋͷUnityϓϩδΣΫτ͸ͪ͜Β͔Βμ΢ϯϩʔυͰ͖·͢ɿ
    https://www.dropbox.com/s/3aokgqnqs05sdyg/UnityDojoSpecial2016Yasuhara.zip?dl=0

    View full-size slide