Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create a script from another engine script

Discussion in 'Scripting' started by najikadri, Feb 13, 2016.

  1. najikadri

    najikadri

    Joined:
    Apr 2, 2015
    Posts:
    14
    Before I used Unity 3D , I used Construct 2 game engine. They have a script for a bullet behavior which I used in my previous game. Now I'm making a sequel of that game but in Unity, and the problem is that I want the same bullet behavior in my unity game. I have the script which is open soucre from the C2 engine but I don't know how to implement it in Unity engine because of the different API. Any suggestions how can I create a script with similar functionality of the other engine script?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Hi,

    need more info.. what does the script do? maybe you can post the script & video to show how it works also.
     
  3. najikadri

    najikadri

    Joined:
    Apr 2, 2015
    Posts:
    14
    @mgear this is the code and I want to be changed into Unity.
    Code (JavaScript):
    1. // ECMAScript 5 strict mode
    2. "use strict";
    3.  
    4. assert2(cr, "cr namespace not created");
    5. assert2(cr.behaviors, "cr.behaviors not created");
    6.  
    7. /////////////////////////////////////
    8. // Behavior class
    9. cr.behaviors.Bullet = function(runtime)
    10. {
    11.     this.runtime = runtime;
    12. };
    13.  
    14. (function ()
    15. {
    16.     var behaviorProto = cr.behaviors.Bullet.prototype;
    17.        
    18.     /////////////////////////////////////
    19.     // Behavior type class
    20.     behaviorProto.Type = function(behavior, objtype)
    21.     {
    22.         this.behavior = behavior;
    23.         this.objtype = objtype;
    24.         this.runtime = behavior.runtime;
    25.     };
    26.  
    27.     var behtypeProto = behaviorProto.Type.prototype;
    28.  
    29.     behtypeProto.onCreate = function()
    30.     {
    31.     };
    32.  
    33.     /////////////////////////////////////
    34.     // Behavior instance class
    35.     behaviorProto.Instance = function(type, inst)
    36.     {
    37.         this.type = type;
    38.         this.behavior = type.behavior;
    39.         this.inst = inst;                // associated object instance to modify
    40.         this.runtime = type.runtime;
    41.     };
    42.  
    43.     var behinstProto = behaviorProto.Instance.prototype;
    44.  
    45.     behinstProto.onCreate = function()
    46.     {
    47.         var speed = this.properties[0];
    48.         this.acc = this.properties[1];
    49.         this.g = this.properties[2];
    50.         this.bounceOffSolid = (this.properties[3] !== 0);
    51.         this.setAngle = (this.properties[4] !== 0);
    52.        
    53.         this.dx = Math.cos(this.inst.angle) * speed;
    54.         this.dy = Math.sin(this.inst.angle) * speed;
    55.         this.lastx = this.inst.x;
    56.         this.lasty = this.inst.y;      
    57.         this.lastKnownAngle = this.inst.angle;
    58.         this.travelled = 0;
    59.        
    60.         this.enabled = (this.properties[5] !== 0);
    61.     };
    62.    
    63.     behinstProto.saveToJSON = function ()
    64.     {
    65.         return {
    66.             "acc": this.acc,
    67.             "g": this.g,
    68.             "dx": this.dx,
    69.             "dy": this.dy,
    70.             "lx": this.lastx,
    71.             "ly": this.lasty,
    72.             "lka": this.lastKnownAngle,
    73.             "t": this.travelled,
    74.             "e": this.enabled
    75.         };
    76.     };
    77.    
    78.     behinstProto.loadFromJSON = function (o)
    79.     {
    80.         this.acc = o["acc"];
    81.         this.g = o["g"];
    82.         this.dx = o["dx"];
    83.         this.dy = o["dy"];
    84.         this.lastx = o["lx"];
    85.         this.lasty = o["ly"];
    86.         this.lastKnownAngle = o["lka"];
    87.         this.travelled = o["t"];
    88.         this.enabled = o["e"];
    89.     };
    90.  
    91.     behinstProto.tick = function ()
    92.     {
    93.         if (!this.enabled)
    94.             return;
    95.            
    96.         var dt = this.runtime.getDt(this.inst);
    97.         var s, a;
    98.         var bounceSolid, bounceAngle;
    99.        
    100.         // Object had its angle changed: change angle of motion, providing 'Set angle' is enabled.
    101.         if (this.inst.angle !== this.lastKnownAngle)
    102.         {
    103.             if (this.setAngle)
    104.             {
    105.                 s = cr.distanceTo(0, 0, this.dx, this.dy);
    106.                 this.dx = Math.cos(this.inst.angle) * s;
    107.                 this.dy = Math.sin(this.inst.angle) * s;
    108.             }
    109.            
    110.             this.lastKnownAngle = this.inst.angle;
    111.         }
    112.        
    113.         // Apply acceleration
    114.         if (this.acc !== 0)
    115.         {
    116.             s = cr.distanceTo(0, 0, this.dx, this.dy);
    117.            
    118.             if (this.dx === 0 && this.dy === 0)
    119.                 a = this.inst.angle;
    120.             else
    121.                 a = cr.angleTo(0, 0, this.dx, this.dy);
    122.                
    123.             s += this.acc * dt;
    124.            
    125.             // Don't decelerate to negative speeds
    126.             if (s < 0)
    127.                 s = 0;
    128.            
    129.             this.dx = Math.cos(a) * s;
    130.             this.dy = Math.sin(a) * s;
    131.         }
    132.        
    133.         // Apply gravity
    134.         if (this.g !== 0)
    135.             this.dy += this.g * dt;
    136.            
    137.         this.lastx = this.inst.x;
    138.         this.lasty = this.inst.y;
    139.        
    140.         // Apply movement to the object
    141.         if (this.dx !== 0 || this.dy !== 0)
    142.         {
    143.             this.inst.x += this.dx * dt;
    144.             this.inst.y += this.dy * dt;
    145.             this.travelled += cr.distanceTo(0, 0, this.dx * dt, this.dy * dt)
    146.            
    147.             if (this.setAngle)
    148.             {
    149.                 this.inst.angle = cr.angleTo(0, 0, this.dx, this.dy);
    150.                 this.inst.set_bbox_changed();
    151.                 this.lastKnownAngle = this.inst.angle;
    152.             }
    153.            
    154.             this.inst.set_bbox_changed();
    155.            
    156.             // Is bouncing off solid and has moved in to a solid
    157.             if (this.bounceOffSolid)
    158.             {
    159.                 bounceSolid = this.runtime.testOverlapSolid(this.inst);
    160.                
    161.                 // Has hit a solid
    162.                 if (bounceSolid)
    163.                 {
    164.                     this.runtime.registerCollision(this.inst, bounceSolid);
    165.                    
    166.                     s = cr.distanceTo(0, 0, this.dx, this.dy);
    167.                     bounceAngle = this.runtime.calculateSolidBounceAngle(this.inst, this.lastx, this.lasty);
    168.                     this.dx = Math.cos(bounceAngle) * s;
    169.                     this.dy = Math.sin(bounceAngle) * s;
    170.                     this.inst.x += this.dx * dt;            // move out for one tick since the object can't have spent a tick in the solid
    171.                     this.inst.y += this.dy * dt;
    172.                     this.inst.set_bbox_changed();
    173.                    
    174.                     if (this.setAngle)
    175.                     {
    176.                         // Setting the object angle after a bounce may cause it to overlap a solid again.
    177.                         // Make sure it's pushed out.
    178.                         this.inst.angle = bounceAngle;
    179.                         this.lastKnownAngle = bounceAngle;
    180.                         this.inst.set_bbox_changed();
    181.                     }
    182.                    
    183.                     // Advance the object until it is outside the solid
    184.                     if (!this.runtime.pushOutSolid(this.inst, this.dx / s, this.dy / s, Math.max(s * 2.5 * dt, 30)))
    185.                         this.runtime.pushOutSolidNearest(this.inst, 100);
    186.                 }
    187.             }
    188.         }
    189.     };
    190.    
    191.     /**BEGIN-PREVIEWONLY**/
    192.     behinstProto.getDebuggerValues = function (propsections)
    193.     {
    194.         propsections.push({
    195.             "title": this.type.name,
    196.             "properties": [
    197.                 {"name": "Vector X", "value": this.dx},
    198.                 {"name": "Vector Y", "value": this.dy},
    199.                 {"name": "Overall speed", "value": cr.distanceTo(0, 0, this.dx, this.dy)},
    200.                 {"name": "Angle of motion", "value":cr.to_degrees(cr.angleTo(0, 0, this.dx, this.dy))},
    201.                 {"name": "Acceleration", "value": this.acc},
    202.                 {"name": "Gravity", "value": this.g},
    203.                 {"name": "Distance travelled", "value": this.travelled, "readonly": true},
    204.                 {"name": "Enabled", "value": this.enabled}
    205.             ]
    206.         });
    207.     };
    208.    
    209.     behinstProto.onDebugValueEdited = function (header, name, value)
    210.     {
    211.         var a, s;
    212.        
    213.         switch (name) {
    214.         case "Vector X":                this.dx = value;                break;
    215.         case "Vector Y":                this.dy = value;                break;
    216.         case "Overall speed":
    217.             a = cr.angleTo(0, 0, this.dx, this.dy);
    218.             this.dx = Math.cos(a) * value;
    219.             this.dy = Math.sin(a) * value;
    220.             break;
    221.         case "Angle of motion":
    222.             a = cr.to_radians(value);
    223.             s = cr.distanceTo(0, 0, this.dx, this.dy)
    224.             this.dx = Math.cos(a) * s;
    225.             this.dy = Math.sin(a) * s;
    226.             break;
    227.         case "Acceleration":            this.acc = value;                break;
    228.         case "Gravity":                    this.g = value;                    break;
    229.         case "Enabled":                    this.enabled = value;            break;
    230.         }
    231.     };
    232.     /**END-PREVIEWONLY**/
    233.  
    234.     //////////////////////////////////////
    235.     // Conditions
    236.     function Cnds() {};
    237.  
    238.     Cnds.prototype.CompareSpeed = function (cmp, s)
    239.     {
    240.         return cr.do_cmp(cr.distanceTo(0, 0, this.dx, this.dy), cmp, s);
    241.     };
    242.    
    243.     Cnds.prototype.CompareTravelled = function (cmp, d)
    244.     {
    245.         return cr.do_cmp(this.travelled, cmp, d);
    246.     };
    247.    
    248.     behaviorProto.cnds = new Cnds();
    249.  
    250.     //////////////////////////////////////
    251.     // Actions
    252.     function Acts() {};
    253.  
    254.     Acts.prototype.SetSpeed = function (s)
    255.     {
    256.         var a = cr.angleTo(0, 0, this.dx, this.dy);
    257.         this.dx = Math.cos(a) * s;
    258.         this.dy = Math.sin(a) * s;
    259.     };
    260.    
    261.     Acts.prototype.SetAcceleration = function (a)
    262.     {
    263.         this.acc = a;
    264.     };
    265.    
    266.     Acts.prototype.SetGravity = function (g)
    267.     {
    268.         this.g = g;
    269.     };
    270.    
    271.     Acts.prototype.SetAngleOfMotion = function (a)
    272.     {
    273.         a = cr.to_radians(a);
    274.         var s = cr.distanceTo(0, 0, this.dx, this.dy)
    275.         this.dx = Math.cos(a) * s;
    276.         this.dy = Math.sin(a) * s;
    277.     };
    278.    
    279.     Acts.prototype.Bounce = function (objtype)
    280.     {
    281.         if (!objtype)
    282.             return;
    283.        
    284.         var otherinst = objtype.getFirstPicked(this.inst);
    285.        
    286.         if (!otherinst)
    287.             return;
    288.            
    289.         var dt = this.runtime.getDt(this.inst);
    290.         var s = cr.distanceTo(0, 0, this.dx, this.dy);
    291.         var bounceAngle = this.runtime.calculateSolidBounceAngle(this.inst, this.lastx, this.lasty, otherinst);
    292.         this.dx = Math.cos(bounceAngle) * s;
    293.         this.dy = Math.sin(bounceAngle) * s;
    294.         this.inst.x += this.dx * dt;            // move out for one tick since the object can't have spent a tick in the solid
    295.         this.inst.y += this.dy * dt;
    296.         this.inst.set_bbox_changed();
    297.        
    298.         if (this.setAngle)
    299.         {
    300.             // Setting the object angle after a bounce may cause it to overlap a solid again.
    301.             // Make sure it's pushed out.
    302.             this.inst.angle = bounceAngle;
    303.             this.lastKnownAngle = bounceAngle;
    304.             this.inst.set_bbox_changed();
    305.         }
    306.        
    307.         // Advance the object until it is outside the solid
    308.         if (this.bounceOffSolid)
    309.         {
    310.             if (!this.runtime.pushOutSolid(this.inst, this.dx / s, this.dy / s, Math.max(s * 2.5 * dt, 30)))
    311.                 this.runtime.pushOutSolidNearest(this.inst, 100);
    312.         }
    313.         else
    314.         {
    315.             this.runtime.pushOut(this.inst, this.dx / s, this.dy / s, Math.max(s * 2.5 * dt, 30), otherinst)
    316.         }
    317.     };
    318.    
    319.     Acts.prototype.SetEnabled = function (en)
    320.     {
    321.         this.enabled = (en === 1);
    322.     };
    323.    
    324.     behaviorProto.acts = new Acts();
    325.  
    326.     //////////////////////////////////////
    327.     // Expressions
    328.     function Exps() {};
    329.  
    330.     Exps.prototype.Speed = function (ret)
    331.     {
    332.         var s = cr.distanceTo(0, 0, this.dx, this.dy);
    333.        
    334.         // Due to floating point inaccuracy is likely to return 99.9999999 when speed is set to 100.
    335.         // So round to nearest millionth of a pixel per second.
    336.         s = cr.round6dp(s);
    337.        
    338.         ret.set_float(s);
    339.     };
    340.    
    341.     Exps.prototype.Acceleration = function (ret)
    342.     {
    343.         ret.set_float(this.acc);
    344.     };
    345.    
    346.     Exps.prototype.AngleOfMotion = function (ret)
    347.     {
    348.         ret.set_float(cr.to_degrees(cr.angleTo(0, 0, this.dx, this.dy)));
    349.     };
    350.    
    351.     Exps.prototype.DistanceTravelled = function (ret)
    352.     {
    353.         ret.set_float(this.travelled);
    354.     };
    355.    
    356.     behaviorProto.exps = new Exps();
    357.    
    358. }());
     
  4. garrett_hogan

    garrett_hogan

    Joined:
    Sep 20, 2015
    Posts:
    1
    Looks like you are just creating a projectile and applying force to it's rigidbody, while also making it bounce off of solid surfaces, and also shoot through some solid surfaces. Is that correct?
     
  5. najikadri

    najikadri

    Joined:
    Apr 2, 2015
    Posts:
    14
    @garretaj yes but I'm not applying force on rigidbody I'm just moving it using the position transform. Even though I do have attached a rigidbody to the bullet.