Advertisement
Guest User

BuildScript.cs

a guest
Nov 8th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BuildScript : MonoBehaviour {
  5.    
  6.     GameObject GroundObject;
  7.     bool onGround;
  8.     Vector3 speed;
  9.     float originalMass;
  10.     System.Single zero = 1e-7f;
  11.     Vector3 rotation;
  12.     Vector3 lastHit;
  13.  
  14.     // Use this for initialization
  15.     void Start () {
  16.         GroundObject = null;
  17.         onGround = false;
  18.         lastHit = new Vector3(0, 0, -100);
  19.     }
  20.  
  21.     void OnCollisionEnter(Collision collision){
  22.  
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void FixedUpdate () {
  27.         speed  = new Vector3(3, 0, 0);
  28.         int x = Screen.width / 2;
  29.         int y = Screen.height / 2;
  30.         Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y));
  31.         RaycastHit hit;
  32.         Transform cam = Camera.main.transform;
  33.         float snap = 15;
  34.  
  35.         // Release object when pressing left mouse button
  36.         if(GroundObject != null && Input.GetMouseButtonDown(0)){
  37.             GroundObject.layer = 0;
  38.             GroundObject.rigidbody.isKinematic = false;
  39.             GroundObject.rigidbody.mass = originalMass;
  40.             GroundObject.rigidbody.freezeRotation = false;
  41.             GroundObject = null;
  42.         }
  43.  
  44.         // Get GameObject when pressing right mouse button
  45.         if(GroundObject == null){
  46.             if(Physics.Raycast(ray, out hit) && hit.distance < 5 && hit.transform.gameObject.rigidbody != null && Input.GetMouseButtonDown(1)){
  47.                 GroundObject = hit.transform.gameObject;
  48.                 originalMass = GroundObject.rigidbody.mass;
  49.                 GroundObject.rigidbody.mass = zero;
  50.                 GroundObject.layer = 2;
  51.                 GroundObject.rigidbody.rotation = Quaternion.identity;
  52.                 GroundObject.rigidbody.freezeRotation = true;
  53.                 GroundObject.rigidbody.isKinematic = true;
  54.             }
  55.         }
  56.  
  57.         if(GroundObject != null){
  58.             if(GroundObject.rigidbody.isKinematic == false){
  59.             GroundObject.rigidbody.velocity = Vector3.zero;
  60.             }
  61.  
  62.             // Calculate GroundObjects position when on ground
  63.                 if (Physics.Raycast(ray, out hit) && hit.distance < 5) {
  64.                     GridController grid = (GridController) GameObject.Find("GridController").GetComponent(typeof(GridController));
  65.                     if (lastHit != hit.point){
  66.                         grid.Generate(hit.point);
  67.                     }
  68.                     lastHit = hit.point;
  69.                     GroundObject.rigidbody.MovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);
  70.                 //  grid.Clear();
  71.                     onGround = true;
  72.                 }else{
  73.                 onGround = false;
  74.             }
  75.  
  76.             // Rotate object with mouse when right mouse button is down
  77.             if(Input.GetMouseButton(1)){
  78.                 if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
  79.                 {
  80.                     Vector3 roundAngles = new Vector3(Input.GetAxis("Mouse Y") * 2, Input.GetAxis("Mouse X") * 2, 0);
  81.                     roundAngles.x = Mathf.Round(roundAngles.x / snap) * snap;
  82.                     roundAngles.y = Mathf.Round(roundAngles.y / snap) * snap;
  83.                     roundAngles.z = Mathf.Round(roundAngles.z / snap) * snap;
  84.                     GroundObject.transform.Rotate(roundAngles, Space.World);
  85.                
  86.                 }
  87.             }
  88.             if(GroundObject.transform.rotation != Quaternion.identity){
  89.                 Vector3 rotation = GroundObject.transform.eulerAngles;
  90.                 rotation.x = Mathf.Round(rotation.x / snap) * snap;
  91.                 rotation.y = Mathf.Round(rotation.y / snap) * snap;
  92.                 rotation.z = Mathf.Round(rotation.z / snap) * snap;
  93.                 GroundObject.transform.eulerAngles = rotation;
  94.             }
  95.             }
  96.     }
  97.  
  98.     void LateUpdate () {
  99.         if(GroundObject != null){
  100.         // Set object in coordinates set in editor
  101.         if(onGround == false){
  102.             foreach (Transform child in transform)
  103.             {
  104.                 if(child.name == "BuildObject_1"){
  105.                     GroundObject.rigidbody.isKinematic = true;
  106.                     GroundObject.transform.position = child.transform.position;            
  107.                 }
  108.             }
  109.         }
  110.         // Snapping coordinates
  111.         if (onGround == true){
  112.        
  113.         }
  114.     }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement