Advertisement
Guest User

Untitled

a guest
Feb 7th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Pig : MonoBehaviour
  6. {
  7.     private float timer;
  8.     bool twosecond = true;
  9.  
  10.     void Start()
  11.     {
  12.     }
  13.  
  14.     void Update()
  15.     {
  16.        
  17.         timer += Time.deltaTime;
  18.        
  19.         if (timer < 2)
  20.         {
  21.             GameObject.Find("Pig").GetComponent<Rigidbody2D>().velocity = new Vector2(1, 0);
  22.         }
  23.         else
  24.         {
  25.             GameObject.Find("Pig").GetComponent<Rigidbody2D>().velocity = new Vector2(-1, 0);
  26.         }
  27.         if (timer >= 4)
  28.         {
  29.             Flip();
  30.             timer = 0;
  31.             twosecond = true;
  32.         }
  33.         if (timer >= 2 && twosecond == true)
  34.         {
  35.             Flip();
  36.             twosecond = false;
  37.         }
  38.        
  39.     }
  40.  
  41.     private void OnCollisionEnter2D(Collision2D collision)
  42.     {
  43.         Rigidbody2D val;
  44.         if(collision.gameObject.TryGetComponent<Rigidbody2D>(out val))
  45.         {
  46.             val.AddForce(new Vector3(-10, 10), ForceMode2D.Impulse);
  47.         }
  48.     }
  49.  
  50.     void Flip()
  51.     {
  52.         Vector3 Scaler = transform.localScale;
  53.         Scaler.x *= -1;
  54.         transform.localScale = Scaler;
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement