Search Unity

Error : Extension method must be defined in a non-generic static class

Discussion in 'Scripting' started by S3Critsss, Jan 6, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    Does anybody know how to fix this movement script. ive tried changing to public static class NinjaMovementScript : MonoBehaviour but it comes up with more errors

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class NinjaMovementScript : MonoBehaviour {
    7.  
    8.     //Player speed and JumpForce. You can tweak these to change the game dynamics.
    9.     public float PlayerSpeed;
    10.     public float JumpForce;
    11.  
    12.     //Do you want player to have double jump? Then make this DoubleJump boolean true :)
    13.     public bool DoubleJump;
    14.  
    15.  
    16.     //These variables are for the code. They track the current events of the player character.
    17.     //You don't need to change or worry about them :)
    18.     private MainEventsLog MainEventsLog_script;
    19.     private bool DJ_available;
    20.     private float JumpForceCount;
    21.     private bool IsGrounded;
    22.  
    23.     public List<GameObject> GroundedToObjectsList;
    24.     public List<GameObject> WalledToObjectsList;
    25.  
    26.     private float walljump_count;
    27.     private bool WallTouch;
    28.     private bool WallGripJustStarted;
    29.  
    30.     private bool PlayerLooksRight;
    31.  
    32.     private bool NoNeedForSafeJump_bool = false;
    33.  
    34.     private int JustPressedSpace;
    35.     private int FixStateTimer = 0;
    36.     private int OnCollisionStayCounter = 0;
    37.     private int OnCollisionBugThreshold = 0;
    38.     private int UpInTheAir_Counter;
    39.  
    40.     private float Ground_X_MIN;
    41.     private float Ground_X_MAX;
    42.     private float Ground_Y_MIN;
    43.     private float Ground_Y_MAX;
    44.  
    45.     //This is to make sure Ninja moves along moving platforms when standing on them.
    46.     public GameObject NinjaPlatformRoot_PREFAB;
    47.     private NinjaPlatformRoot NinjaPlatformRoot;
    48.     public GameObject NinjaVisualRoot;
    49.  
    50.     //Checkpoint related things:
    51.     public GameObject ActiveCheckpoint;
    52.  
    53.  
    54.  
    55.     //These booleans keep track which button is being pressed or not.
    56.     private bool Btn_Left_bool;
    57.     private bool Btn_Right_bool;
    58.     private bool Btn_Jump_bool;
    59.  
    60.     //Here are reference slots for AnimationController and Player Sprite Object.
    61.     public Animator AnimatorController;
    62.     public GameObject MySpriteOBJ;
    63.     private Vector3 MySpriteOriginalScale;
    64.  
    65.     //Here are reference slots for Player Particle Emitters
    66.     public ParticleSystem WallGripParticles;
    67.     private int WallGripEmissionRate;
    68.     public ParticleSystem JumpParticles_floor;
    69.     public ParticleSystem JumpParticles_wall;
    70.     public ParticleSystem JumpParticles_doublejump;
    71.     public ParticleSystem Particles_DeathBoom;
    72.  
    73.  
    74.     //AudioSources play the audios of the scene.
    75.     public AudioSource AudioSource_Jump;
    76.  
    77.  
    78.  
    79.  
    80.     // Use this for initialization
    81.     void Start () {
    82.  
    83.         GroundedToObjectsList = new List<GameObject> ();
    84.         WalledToObjectsList = new List<GameObject> ();
    85.  
    86.         //These define when collision is detected as a floor and when as a wall.
    87.         Ground_X_MIN = -0.75f;
    88.         Ground_X_MAX = 0.75f;
    89.         Ground_Y_MIN = 0.5f;
    90.         Ground_Y_MAX = 1f;
    91.  
    92.         //NinjaRootOBJ makes sure that Ninja together with moving platforms
    93.         GameObject NinjaROOTOBJ = Instantiate (NinjaPlatformRoot_PREFAB, Vector3.zero, Quaternion.identity) as GameObject;
    94.         NinjaPlatformRoot = NinjaROOTOBJ.GetComponent<NinjaPlatformRoot> ();
    95.         NinjaROOTOBJ.name = "NINJA_PlatformRoot";
    96.  
    97.         //Just some default values for WallGrip Particle Emitter.
    98.         WallGripEmissionRate = 10;
    99.         WallGripParticles.emissionRate = 0;
    100.  
    101.         //Player characters looks right in the start of the scene.
    102.         PlayerLooksRight = true;
    103.         MySpriteOriginalScale = MySpriteOBJ.transform.localScale;
    104.  
    105.     }
    106.  
    107.  
    108.  
    109.     // Update is called once per frame
    110.     void Update () {
    111.  
    112.         //Button commands from the keyboard
    113.         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
    114.             Button_Left_press();      
    115.         }
    116.         if(Input.GetKeyUp (KeyCode.LeftArrow)) {
    117.             Button_Left_release();      
    118.         }
    119.  
    120.         if (Input.GetKeyDown (KeyCode.RightArrow)) {
    121.             Button_Right_press();      
    122.         }
    123.         if(Input.GetKeyUp (KeyCode.RightArrow)) {
    124.             Button_Right_release();      
    125.         }
    126.  
    127.         if (Input.GetKeyDown (KeyCode.Space)) {
    128.             JustPressedSpace = 2;
    129.             Button_Jump_press();      
    130.         }
    131.         if (Input.GetKeyUp (KeyCode.Space)) {
    132.             Button_Jump_release();      
    133.         }
    134.  
    135.         if (Input.GetKeyDown (KeyCode.A)) {
    136.             Button_Jump_press();      
    137.         }
    138.         if (Input.GetKeyUp (KeyCode.A)) {
    139.             Button_Jump_release();      
    140.         }
    141.  
    142.         if (walljump_count >= 0) {
    143.             walljump_count -= Time.deltaTime;      
    144.         }
    145.  
    146.     }
    147.  
    148.  
    149.  
    150.  
    151.     void FixedUpdate(){
    152.  
    153.         //The actual Left/Right movement happens here.
    154.  
    155.         //This checks is the player pressing left or right button.
    156.         if(Btn_Left_bool == true && Btn_Right_bool == false){
    157.             if(PlayerLooksRight == true && WallTouch == false){
    158.                 PlayerLooksRight = false;
    159.                 MySpriteOBJ.transform.localScale = new Vector3(-MySpriteOriginalScale.x,MySpriteOriginalScale.y,MySpriteOriginalScale.z);
    160.             }
    161.  
    162.             this.rigidbody2D.AddForce(new Vector2(NinjaVisualRoot.transform.right.x,NinjaVisualRoot.transform.right.y)*-PlayerSpeed*Time.deltaTime);
    163.  
    164.         }else if(Btn_Left_bool == false && Btn_Right_bool == true){
    165.             if(PlayerLooksRight == false && WallTouch == false){
    166.                 PlayerLooksRight = true;
    167.                 MySpriteOBJ.transform.localScale = MySpriteOriginalScale;
    168.             }
    169.  
    170.             this.rigidbody2D.AddForce(new Vector2(NinjaVisualRoot.transform.right.x,NinjaVisualRoot.transform.right.y)*PlayerSpeed*Time.deltaTime);
    171.  
    172.         }
    173.  
    174.         //this makes sure player is not sliding on slobes
    175.         if (IsGrounded == true && WallTouch == false) {
    176.             this.rigidbody2D.gravityScale = 0f;
    177.         } else {
    178.             if(this.rigidbody2D.gravityScale != 1f){
    179.                 this.rigidbody2D.gravityScale = 1f;
    180.             }
    181.         }
    182.  
    183.         //Slowdown the player fall if touching a wall.
    184.         if (IsGrounded == false && WallTouch == true) {
    185.             this.rigidbody2D.velocity = new Vector2 (this.rigidbody2D.velocity.x, Physics2D.gravity.y * 0.01f);
    186.         }
    187.  
    188.         //If Ninja is in the air. Start to totate him back upwards after few frames.
    189.         UpInTheAir_Counter += 1;
    190.         if(UpInTheAir_Counter > 5){
    191.             if (IsGrounded == false && WallTouch == false) {
    192.                 Vector2 RealDirectionV2 = new Vector2(this.transform.up.x,this.transform.up.y);
    193.                 Vector2 WorldUpVec = new Vector2(0f,1f);
    194.                 float TorqueTo = Vector2.Angle (WorldUpVec, RealDirectionV2);
    195.                 if (WorldUpVec.normalized.x > RealDirectionV2.normalized.x) {
    196.                     TorqueTo = TorqueTo * (-1);
    197.                 }
    198.                 if (-WorldUpVec.normalized.y > RealDirectionV2.normalized.y) {
    199.                     TorqueTo = TorqueTo * (-1);
    200.                 }
    201.                 this.rigidbody2D.AddTorque (TorqueTo * 400f * Time.deltaTime);
    202.             }
    203.         }
    204.  
    205.         //Lift player up if jump is happening.
    206.         if (Btn_Jump_bool == true && JumpForceCount > 0) {
    207.             this.rigidbody2D.velocity = new Vector2(this.rigidbody2D.velocity.x,JumpForce);
    208.             JumpForceCount -= 0.1f*Time.deltaTime;          
    209.         }
    210.  
    211.  
    212.         //This if-statement makes sure Ninja is not grounded to any platform when there is no collision detections. (In some cases OnCollisionExit messages might be lost. This makes sure that it will not cause a bug.)
    213.         //START-------
    214.         if (OnCollisionStayCounter == 0) {
    215.             OnCollisionBugThreshold += 1;
    216.         } else {
    217.             OnCollisionStayCounter = 0;
    218.         }
    219.      
    220.         if (OnCollisionBugThreshold > 4 && (IsGrounded == true || WallTouch == true)) {
    221.             DJ_available = true;
    222.             IsGrounded = false;
    223.             WallTouch = false;
    224.             this.transform.parent = null;
    225.             GroundedToObjectsList.Clear();
    226.             WalledToObjectsList.Clear();
    227.             WallGripParticles.emissionRate = 0;
    228.             FixStateTimer = 0;
    229.             OnCollisionBugThreshold = 0;
    230.             OnCollisionStayCounter = 1;
    231.         }
    232.         //--------END
    233.  
    234.  
    235.         float AnimVelY = this.rigidbody2D.velocity.y;
    236.         float AnimVelX = this.rigidbody2D.velocity.sqrMagnitude * 4f;
    237.  
    238.         if (JustPressedSpace > 0) {
    239.             AnimVelX = 0f;
    240.             JustPressedSpace -= 1;
    241.         }
    242.  
    243.         if(Btn_Jump_bool == false && IsGrounded == true){
    244.             //-- Set to zero to get run animation instead of fall animation
    245.             AnimVelY = 0f;
    246.         }
    247.  
    248.         //Send variables to Animation Controller
    249.         AnimatorController.SetFloat ("HorizontalSpeed", AnimVelX);
    250.         AnimatorController.SetFloat ("VerticalSpeed", AnimVelY);
    251.         AnimatorController.SetBool ("Grounded", IsGrounded);
    252.         AnimatorController.SetBool ("Walled", WallTouch);
    253.  
    254.     }
    255.  
    256.  
    257.  
    258.  
    259.  
    260.  
    261.     void OnCollisionEnter2D(Collision2D coll) {
    262.  
    263.         if(coll.gameObject.CompareTag("Enemy")){
    264.             return;
    265.         }
    266.  
    267.         //This makes sure Ninja doesn't slide from previous force when hitting platform. Unless player is holding Left or Right button.
    268.         if (IsGrounded == false && Btn_Left_bool == false && Btn_Right_bool == false) {
    269.             this.rigidbody2D.velocity = new Vector2 (this.rigidbody2D.velocity.x * 0.25f, -0.01f);
    270.         } else if (IsGrounded == false) {
    271.             this.rigidbody2D.velocity = new Vector2 (this.rigidbody2D.velocity.x, -0.01f);  
    272.         }
    273.  
    274.         OnCollisionStayCounter += 1;
    275.         OnCollisionBugThreshold = 0;
    276.         UpInTheAir_Counter = 0;
    277.  
    278.         foreach (ContactPoint2D contact in coll.contacts) {
    279.  
    280.             //If Ninja hits his head to the roof. Stop Jump Force.
    281.             if (0.1f > contact.normal.y && ((contact.normal.x*contact.normal.x) < (0.85f*0.85f))) {
    282.                 JumpForceCount = 0f;
    283.             }
    284.  
    285.             //If it wasn't the roof. Was it a ground perhaps?
    286.             else if (contact.normal.x >= Ground_X_MIN && contact.normal.x <= Ground_X_MAX && contact.normal.y >= Ground_Y_MIN && contact.normal.y <= Ground_Y_MAX) {
    287.  
    288.                 int CountHappenings = 0;
    289.                 foreach(GameObject GroundedObject in GroundedToObjectsList){
    290.                     if(contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID()){
    291.                         CountHappenings += 1;
    292.                     }
    293.                 }
    294.  
    295.                 //Is the platform already listed in GroundedObjects? If not Add it to the list.
    296.                 if(CountHappenings == 0){
    297.                     DJ_available = false;
    298.                     GroundedToObjectsList.Add(contact.collider.gameObject);
    299.                     //Move NinjaPlatformRoot to the new platform.
    300.                     this.transform.parent = null;
    301.                     NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
    302.                     NinjaPlatformRoot.RootedTo = contact.collider.gameObject;
    303.                     this.transform.parent = NinjaPlatformRoot.transform;
    304.  
    305.                     IsGrounded = true;
    306.  
    307.                     this.rigidbody2D.AddForce (contact.normal * (-300f));
    308.  
    309.                     if(WallTouch == true){
    310.                         WallGripParticles.emissionRate = 0;
    311.                         FixStateTimer = 0;
    312.                     }
    313.                 }
    314.  
    315.             //If it wasnt a roof or a ground it must have been wall. No need for Normal check anymore.
    316.             }else{
    317.                 //Ninja must be faling downwards to grab the wall.
    318.                 if (this.rigidbody2D.velocity.y < 0f && IsGrounded == false) {
    319.                     this.rigidbody2D.velocity = Vector2.zero;
    320.                     //is the Object already listed in WalledObjects?
    321.                     int CountHappenings = 0;
    322.                     foreach(GameObject WalledObject in WalledToObjectsList){
    323.                         if(contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID()){
    324.                             CountHappenings += 1;
    325.                         }
    326.                     }
    327.                     //if not. Lets list it.
    328.                     if(CountHappenings == 0){
    329.                         DJ_available = false;
    330.                         WalledToObjectsList.Add(contact.collider.gameObject);
    331.                         this.transform.parent = null;
    332.                         NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
    333.                         NinjaPlatformRoot.RootedTo = contact.collider.gameObject;
    334.                         this.transform.parent = NinjaPlatformRoot.transform;
    335.                  
    336.                         WallTouch = true;
    337.                  
    338.                         //Check that the player is facing to the right direction
    339.                         if (contact.normal.x > 0) {
    340.                             PlayerLooksRight = true;
    341.                             MySpriteOBJ.transform.localScale = MySpriteOriginalScale;
    342.                         } else {
    343.                             PlayerLooksRight = false;
    344.                             MySpriteOBJ.transform.localScale = new Vector3 (-MySpriteOriginalScale.x, MySpriteOriginalScale.y, MySpriteOriginalScale.z);
    345.                         }
    346.                  
    347.                         //Start emiting smoke particles when touching the wall
    348.                         WallGripParticles.emissionRate = WallGripEmissionRate;
    349.  
    350.                     }
    351.                 }
    352.             }
    353.         }
    354.     }
    355.  
    356.  
    357.  
    358.     void OnCollisionStay2D(Collision2D coll) {
    359.  
    360.         OnCollisionStayCounter += 1;
    361.         UpInTheAir_Counter = 0;
    362.  
    363.         //This is making sure that when Ninja is colliding with something it is always registered.
    364.         if (IsGrounded == false && WallTouch == false) {
    365.             FixStateTimer += 1;
    366.             if(FixStateTimer > 4){
    367.                 foreach (ContactPoint2D contact in coll.contacts) {
    368.                     if (0.1f > contact.normal.y && ((contact.normal.x*contact.normal.x) < (0.85f*0.85f))) {
    369.                         JumpForceCount = 0f;
    370.                     }
    371.                     else if (contact.normal.x >= Ground_X_MIN && contact.normal.x <= Ground_X_MAX && contact.normal.y >= Ground_Y_MIN && contact.normal.y <= Ground_Y_MAX) {
    372.                         FixStateTimer = 0;
    373.                         DJ_available = false;
    374.                         GroundedToObjectsList.Add(contact.collider.gameObject);
    375.                         IsGrounded = true;
    376.                     }else{
    377.                      
    378.                         if (this.rigidbody2D.velocity.y < 0f) {
    379.                             FixStateTimer = 0;
    380.                             DJ_available = false;
    381.                             WalledToObjectsList.Add(contact.collider.gameObject);
    382.                             WallTouch = true;
    383.  
    384.                             this.transform.parent = null;
    385.                             NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
    386.                             NinjaPlatformRoot.RootedTo = contact.collider.gameObject;
    387.                             this.transform.parent = NinjaPlatformRoot.transform;
    388.                          
    389.                             if (contact.normal.x > 0) {
    390.                                 PlayerLooksRight = true;
    391.                                 MySpriteOBJ.transform.localScale = MySpriteOriginalScale;
    392.                             } else {
    393.                                 PlayerLooksRight = false;
    394.                                 MySpriteOBJ.transform.localScale = new Vector3 (-MySpriteOriginalScale.x, MySpriteOriginalScale.y, MySpriteOriginalScale.z);
    395.                             }
    396.                          
    397.                             //Start emiting smoke particles when touching the wall
    398.                             WallGripParticles.emissionRate = WallGripEmissionRate;
    399.                         }
    400.                     }
    401.                 }
    402.             }
    403.         }
    404.  
    405.  
    406.         //OnStay Ground Events:
    407.         else if (IsGrounded == true) {
    408.             Vector2 NinjaStandDirection = Vector2.zero;
    409.             foreach (ContactPoint2D contact in coll.contacts) {
    410.                 int CountHappenings = 0;
    411.                 foreach (GameObject GroundedObject in GroundedToObjectsList) {
    412.                     if (contact.collider.gameObject.GetInstanceID () == GroundedObject.GetInstanceID ()) {
    413.                         NinjaStandDirection += contact.normal;
    414.                         CountHappenings += 1;
    415.                     }
    416.                 }
    417.                 if (CountHappenings > 0) {
    418.                     NinjaStandDirection = NinjaStandDirection/CountHappenings;
    419.                     //This makes sure that Ninja doesn't walk on the walls.
    420.                     if((NinjaStandDirection.x > Ground_X_MAX || NinjaStandDirection.x < Ground_X_MIN) && (NinjaStandDirection.y > Ground_Y_MAX || NinjaStandDirection.y < Ground_Y_MIN)){
    421.                         this.rigidbody2D.AddForce(NinjaStandDirection * 100f);
    422.                     }else{
    423.                         //this Rotates the Ninja to allign with platform.
    424.                         Vector2 RealDirectionV2 = new Vector2(this.transform.up.x,this.transform.up.y);
    425.                         float TorqueTo = Vector2.Angle (NinjaStandDirection, RealDirectionV2);
    426.                         if (NinjaStandDirection.normalized.x > RealDirectionV2.normalized.x) {
    427.                             TorqueTo = TorqueTo * (-1);
    428.                         }
    429.                         if (-NinjaStandDirection.normalized.y > RealDirectionV2.normalized.y) {
    430.                             TorqueTo = TorqueTo * (-1);
    431.                         }
    432.                         this.rigidbody2D.AddTorque (TorqueTo * 1000f * Time.deltaTime);
    433.  
    434.                         this.rigidbody2D.AddForce (NinjaStandDirection * (-300f));
    435.                     }
    436.                 }
    437.             }
    438.  
    439.  
    440.         //OnStay Wall Events:
    441.         } else if (WallTouch == true) {
    442.  
    443.             foreach (ContactPoint2D contact in coll.contacts) {
    444.                 Vector2 NinjaWallDirection = Vector2.zero;
    445.                 int CountHappenings = 0;
    446.                 foreach (GameObject WallObject in WalledToObjectsList) {
    447.                     if (contact.collider.gameObject.GetInstanceID () == WallObject.GetInstanceID ()) {
    448.                         NinjaWallDirection += contact.normal;
    449.                         CountHappenings += 1;
    450.                     }
    451.                 }
    452.              
    453.                 if (CountHappenings > 0) {
    454.                     NinjaWallDirection = NinjaWallDirection/CountHappenings;
    455.                     if((NinjaWallDirection.x > Ground_X_MAX || NinjaWallDirection.x < Ground_X_MIN) && (NinjaWallDirection.y > Ground_Y_MAX || NinjaWallDirection.y < Ground_Y_MIN)){
    456.  
    457.                         if((Btn_Left_bool == false && PlayerLooksRight == false) || (Btn_Right_bool == false && PlayerLooksRight == true)){
    458.                             this.rigidbody2D.AddForce (NinjaWallDirection * -100f);
    459.                         }
    460.                         //this Rotates the Ninja to allign with the wall.
    461.                         Vector2 RealDirectionV2 = new Vector2(this.transform.up.x,this.transform.up.y);
    462.  
    463.                         if(PlayerLooksRight == false){
    464.                             RealDirectionV2 = RotateThisVector(RealDirectionV2,1.35f);
    465.                         }else{
    466.                             RealDirectionV2 = RotateThisVector(RealDirectionV2,-1.35f);
    467.                         }
    468.  
    469.                         float TorqueTo = Vector2.Angle (NinjaWallDirection, RealDirectionV2);
    470.                         if (contact.normal.x > RealDirectionV2.normalized.x) {
    471.                             TorqueTo = TorqueTo * (-1);
    472.                         }
    473.                         if (-contact.normal.y > RealDirectionV2.normalized.y) {
    474.                             TorqueTo = TorqueTo * (-1);
    475.                         }
    476.                         this.rigidbody2D.AddTorque (TorqueTo * 450f * Time.deltaTime);
    477.                     }else{
    478.                         if((Btn_Left_bool == false && PlayerLooksRight == false) || (Btn_Right_bool == false && PlayerLooksRight == true)){
    479.                             this.rigidbody2D.AddForce (NinjaWallDirection * 100f);
    480.                         }
    481.                     }
    482.                 }
    483.             }
    484.         }
    485.     }
    486.      
    487.  
    488.     //Here we check if the player is jumping or moving away from the wall or ground.
    489.     void OnCollisionExit2D(Collision2D coll) {
    490.         OnCollisionStayCounter = 0;
    491.         foreach (ContactPoint2D contact in coll.contacts) {
    492.             int CountHappenings = 0;
    493.             int CountHappeningsWALL = 0;
    494.             foreach(GameObject GroundedObject in GroundedToObjectsList){
    495.                 if(contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID()){
    496.                     CountHappenings += 1;
    497.                 }
    498.             }
    499.             foreach(GameObject WalledObject in WalledToObjectsList){
    500.                 if(contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID()){
    501.                     CountHappeningsWALL += 1;
    502.                 }
    503.             }
    504.  
    505.             //was the object one of the grounded to objects?
    506.             if(CountHappenings > 0){
    507.                 GroundedToObjectsList.Remove(contact.collider.gameObject);
    508.                 if(GroundedToObjectsList.Count == 0){
    509.                     DJ_available = true;
    510.                     IsGrounded = false;
    511.                     this.transform.parent = null;
    512.                     FixStateTimer = 0;
    513.                 }
    514.             }
    515.  
    516.             //was the object one of the wall?
    517.             if(CountHappeningsWALL > 0){
    518.                 WalledToObjectsList.Remove(contact.collider.gameObject);
    519.                 if(WalledToObjectsList.Count == 0){
    520.                     if(NoNeedForSafeJump_bool == false){
    521.                         //This makes the walljump a bit easier. Player is able to do the wall jump even few miliseconds after he let go of the wall.
    522.                         walljump_count = 0.16f;
    523.                     }
    524.                     NoNeedForSafeJump_bool = false;
    525.                     DJ_available = true;
    526.                     this.transform.parent = null;
    527.                     WallTouch = false;
    528.                     WallGripParticles.emissionRate = 0;
    529.                     FixStateTimer = 0;
    530.                 }
    531.             }
    532.         }
    533.     }
    534.  
    535.  
    536.     public void NinjaDies(){
    537.         Particles_DeathBoom.Emit (50);
    538.  
    539.         this.gameObject.transform.position = ActiveCheckpoint.transform.position;
    540.         this.rigidbody2D.velocity = Vector2.zero;
    541.  
    542.         //Send message to MainEventsLog. First checks if the reference path is set. If not, it will MainEventsLog from the scene.
    543.         if(MainEventsLog_script == null){
    544.             MainEventsLog_script = GameObject.FindGameObjectWithTag("MainEventLog").GetComponent<MainEventsLog>();
    545.         }
    546.         MainEventsLog_script.PlayerDied();
    547.     }
    548.  
    549.     public void NinjaKilledEnemy(){
    550.         GroundedToObjectsList.Clear ();
    551.         WalledToObjectsList.Clear ();
    552.     }
    553.  
    554.     //This region is for Button events. (These same events are called from Keyboard and Touch Buttons)
    555.     #region ButtonVoids
    556.  
    557.     public void Button_Left_press(){
    558.         Btn_Left_bool = true;
    559.     }
    560.  
    561.     public void Button_Left_release(){
    562.         Btn_Left_bool = false;
    563.     }
    564.  
    565.     public void Button_Right_press(){
    566.         Btn_Right_bool = true;
    567.     }
    568.      
    569.     public void Button_Right_release(){
    570.         Btn_Right_bool = false;
    571.     }
    572.  
    573.     public void Button_Jump_press(){
    574.  
    575.         Btn_Jump_bool = true;
    576.  
    577.         //If you are on the ground. Do the Jump.
    578.         if (IsGrounded == true) {
    579.             DJ_available = true;
    580.             AudioSource_Jump.Play();
    581.             JumpForceCount = 0.02f;
    582.             this.rigidbody2D.velocity = new Vector2(this.rigidbody2D.velocity.x,0f) + new Vector2(NinjaVisualRoot.transform.up.x,NinjaVisualRoot.transform.up.y)*JumpForce;
    583.  
    584.             JumpParticles_floor.Emit(20);
    585.  
    586.         //If you are in the air and DoubleJump is available. Do it!
    587.         }else if(DoubleJump == true && DJ_available == true && WallTouch == false){
    588.             DJ_available = false;
    589.             AudioSource_Jump.Play();
    590.             JumpForceCount = 0.02f;
    591.             this.rigidbody2D.velocity = new Vector2(this.rigidbody2D.velocity.x,JumpForce);
    592.             JumpParticles_doublejump.Emit(10);
    593.         }
    594.  
    595.  
    596.         //If you touch the wall or just let go. And are defenitly not in the ground. Do the Wall Jump!
    597.         if ((WallTouch == true || walljump_count > 0f) && IsGrounded == false) {
    598.  
    599.             //This is to fix the bug where Ninja was sometimes able to do double jump when leaving the wall.
    600.             if(walljump_count <= 0f){
    601.                 NoNeedForSafeJump_bool = true;
    602.             }
    603.  
    604.             DJ_available = true;
    605.             AudioSource_Jump.Play();
    606.             WallTouch = false;
    607.             JumpForceCount = 0.02f;
    608.             JumpParticles_wall.Emit(20);
    609.             this.rigidbody2D.velocity = Vector2.zero;
    610.             if(PlayerLooksRight == false){
    611.                 this.rigidbody2D.AddForce (new Vector2 (-JumpForce*32f, 0f));
    612.             }else{
    613.                 this.rigidbody2D.AddForce (new Vector2 (JumpForce*32f, 0f));
    614.             }
    615.         }
    616.     }
    617.  
    618.     public void Button_Jump_release(){
    619.         JumpForceCount = 0f;
    620.         Btn_Jump_bool = false;
    621.     }
    622.  
    623.     //This is a vector rotator. It can be used to rotate a Vector2 with an angle valua.
    624.     private Vector3 RotateThisVector( this Vector2 v, float angle )
    625.     {
    626.         float sin = Mathf.Sin( angle );
    627.         float cos = Mathf.Cos( angle );
    628.  
    629.         float tx = v.x;
    630.         float ty = v.y;
    631.         v.x = (cos * tx) - (sin * ty);
    632.         v.y = (cos * ty) + (sin * tx);
    633.      
    634.         return v;
    635.     }
    636.  
    637.     #endregion
    638.  
    639.  
    640. }
     
    Last edited: Jan 6, 2020
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,396
    Your problem is the vector3 extension function RotateThisVector at the end of your class.

    remove the this in the arguments or move that function to a static class
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    ive moved it into a static class but it now says 'RotateThisVector' : Cannot declare instance members in a static class
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    988
    Code (CSharp):
    1. v.x = (cos * tx) - (sin * ty);
    You can't change x/y/z of vectors directly like that, you must create a new vector and overwrite the previous vector with the new vector.

    Code (CSharp):
    1. //float x = (cos * tx) - (sin * ty);
    2. //float y = (cos * ty) + (sin * tx);
    3. //Vector2 tmpVect = new Vector2(x, y);
    4. Vector2 tmpVect = new Vector2((cos * tx) - (sin * ty), (cos * ty) + (sin * tx));
    5.  
    6. v = tmpVect;
    Also, your function signature is wrong in a number of ways:
    -Your function signature says you are returning a Vector3, but you're returning a Vector2.
    -Your function needs to be static.
    -Your function should be internal (if you want to restrict it to your namespace), or it should be public. That way you can access the extension method throughout unrelated code that has nothing to do with your static class.

    Code (CSharp):
    1. private Vector3 RotateThisVector(this Vector2 v, float angle)
    Try
    Code (CSharp):
    1. Public Static Vector2 RotateThisVector(this Vector2 v, float angle)
     
    Last edited: Jan 6, 2020
  5. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    sorry im not good at coding but my class looks like this

    public class v3id

    {

    //This is a vector rotator. It can be used to rotate a Vector2 with an angle valua.
    internal static Vector2 RotateThisVector(this Vector2 v, float angle)
    {
    Vector2 tmpVect = new Vector2((cos * tx) - (sin * ty), (cos * ty) + (sin * tx));
    v = tmpVect;

    float sin = Mathf.Sin(angle);
    float cos = Mathf.Cos(angle);

    float tx = v.x;
    float ty = v.y;
    v.x = (cos * tx) - (sin * ty);
    v.y = (cos * ty) + (sin * tx);

    return v;
    }

    }

    it still says that it needs a non-generic static class.
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    988
    You put extension methods in their own special static class.

    Code (CSharp):
    1. public static class ExtensionMethods {
    2.   Public Static Vector2 RotateThisVector(this Vector2 v, float angle) {
    3.     //...
    4.   }
    5. }
    Or you could group them by type, if you have many extension methods based on type and you want to keep them very organised, you could have a folder called ExtensionMethods, and you could have a class for each type

    Code (CSharp):
    1. public static class Vector2_ExtensionMethods {
    2.   Public Static Vector2 RotateThisVector(this Vector2 v, float angle){
    3.     //...
    4.   }
    5.   //Other vector2 extension methods
    6. }

    Please also see this post about code tags, for posting code on the forum.
    https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Last edited: Jan 6, 2020
  7. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    im really sorry but i dont understand, what should my code look like, is it supposed to look like this

    also when the class is set to static it comes up with 86 more errors

    Code (CSharp):
    1.  
    2. public static class ExtensionMethods
    3. {
    4.     public static Vector2 RotateThisVector(this Vector2 v, float angle)
    5.     {
    6.         Vector2 tmpVect = new Vector2((cos * tx) - (sin * ty), (cos * ty) + (sin * tx));
    7.         v = tmpVect;
    8.         float sin = Mathf.Sin(angle);
    9.         float cos = Mathf.Cos(angle);
    10.  
    11.         float tx = v.x;
    12.         float ty = v.y;
    13.         v.x = (cos * tx) - (sin * ty);
    14.         v.y = (cos * ty) + (sin * tx);
    15.  
    16.         return v;
    17.     }
    18. }
     
    Last edited: Jan 6, 2020
  8. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    988
    Code (CSharp):
    1.     public static class ExtensionMethods
    2.     {
    3.         public static Vector2 RotateThisVector(this Vector2 v, float angle)
    4.         {
    5.             float sin = Mathf.Sin(angle);
    6.             float cos = Mathf.Cos(angle);
    7.  
    8.             Vector2 rotatedVect = new Vector2((cos * v.x) - (sin * v.y), (cos * v.y) + (sin * v.x));
    9.  
    10.             return rotatedVect;
    11.         }
    12.     }
    Code (CSharp):
    1. //In any script in your project
    2.  
    3. Vector2 v2 = new Vector2(1.0f, 1.0f);
    4. v2.RotateThisVector(...
     
  9. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    when i make the class static it brings up other errors what should i do??
     
  10. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    91
    Code (CSharp):
    1.  
    2. Vector2 v2 = new Vector2(1.0f, 1.0f);
    3.  
    it says a namespace cannot directly contain members such as fields or methods and when i do this
    Code (csharp):
    1. v2.RotateThisVector
    it shows more errors
     
    Last edited: Jan 7, 2020
unityunity