IRCSS

Procedural sheep

Jul 17th, 2019
213
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SheepMaker : MonoBehaviour {
  6.  
  7.     public static Material legsMat;
  8.  
  9.     private GameObject leftFront;
  10.     private GameObject leftBack;
  11.     private GameObject rightFront;
  12.     private GameObject rightback;
  13.  
  14.     private GameObject head;
  15.  
  16.     private float timer;
  17.  
  18.     private Vector3 deltaMovement;
  19.     private Vector3 prePositon;
  20.  
  21.     // Use this for initialization
  22.     void Start () {
  23.         if (legsMat == null) (legsMat = new Material( this.GetComponent<Renderer>().sharedMaterial)).color = new Color(0.6f, 0.4f, 0.0f);
  24.  
  25.         leftBack = SetupLeg();
  26.         leftFront = SetupLeg();
  27.         rightFront = SetupLeg();
  28.         rightback = SetupLeg();
  29.  
  30.         float xzOffset = 0.35f;
  31.         float yOffset = 0.65f;
  32.         leftBack.transform.localPosition = new Vector3(-xzOffset, -yOffset, -xzOffset);
  33.         leftFront.transform.localPosition = new Vector3(-xzOffset, -yOffset, xzOffset);
  34.         rightFront.transform.localPosition = new Vector3(xzOffset, -yOffset, xzOffset);
  35.         rightback.transform.localPosition = new Vector3(xzOffset, -yOffset, -xzOffset);
  36.  
  37.         head = GameObject.CreatePrimitive(PrimitiveType.Cube);
  38.         head.GetComponent<Renderer>().sharedMaterial = this.GetComponent<Renderer>().sharedMaterial;
  39.         head.transform.parent = this.transform;
  40.         head.transform.localPosition = new Vector3(0f, 0.2f, 0.7f);
  41.         head.transform.localScale = new Vector3(0.36f, 0.36f, 0.36f);
  42.  
  43.     }
  44.  
  45.  
  46.    
  47.     GameObject SetupLeg()
  48.     {
  49.         GameObject t = GameObject.CreatePrimitive(PrimitiveType.Cube);
  50.         t.GetComponent<Renderer>().sharedMaterial = legsMat;
  51.         t.transform.parent = this.transform;
  52.         t.transform.localScale = new Vector3(0.2f, 0.5f, 0.2f);
  53.         return t;
  54.    
  55.     }
  56.    
  57.     // Update is called once per frame
  58.     void Update () {
  59.  
  60.         deltaMovement = this.transform.position - prePositon;
  61.         prePositon = this.transform.position;
  62.  
  63.  
  64.         if (deltaMovement.magnitude <= 0.0f) return;
  65.  
  66.         timer += Time.deltaTime;
  67.  
  68.         float rotateBatchOne = (Mathf.Abs(((timer * 1.5f %1)*2f)-1f)-0.5f)*4f ;
  69.         Vector3 rotationCenter = leftBack.transform.localToWorldMatrix *  
  70.             new Vector4(leftBack.transform.localPosition.x, leftBack.transform.localPosition.y+2.15f, leftBack.transform.localPosition.z,1f);
  71.  
  72.         leftBack.transform.RotateAround(rotationCenter, leftBack.transform.right, rotateBatchOne);
  73.         rotationCenter = rightFront.transform.localToWorldMatrix *
  74.             new Vector4(rightFront.transform.localPosition.x, rightFront.transform.localPosition.y + 2.15f, rightFront.transform.localPosition.z, 1f);
  75.         rightFront.transform.RotateAround(rotationCenter, rightFront.transform.right, rotateBatchOne);
  76.  
  77.         float rotateBatchTwo = (Mathf.Abs((((timer * 1.5f+ 0.5f )% 1) * 2f) - 1f) - 0.5f) * 4f;
  78.  
  79.         rotationCenter = rightback.transform.localToWorldMatrix *
  80.             new Vector4(rightback.transform.localPosition.x, rightback.transform.localPosition.y + 2.15f, rightback.transform.localPosition.z, 1f);
  81.  
  82.         rightback.transform.RotateAround(rotationCenter, rightback.transform.right, rotateBatchTwo);
  83.         rotationCenter = leftFront.transform.localToWorldMatrix *
  84.             new Vector4(leftFront.transform.localPosition.x, leftFront.transform.localPosition.y + 2.15f, leftFront.transform.localPosition.z, 1f);
  85.         leftFront.transform.RotateAround(rotationCenter, leftFront.transform.right, rotateBatchTwo);
  86.  
  87.     }
  88. }
RAW Paste Data