Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class BuildScript : MonoBehaviour {
- GameObject GroundObject;
- bool onGround;
- Vector3 speed;
- float originalMass;
- System.Single zero = 1e-7f;
- Vector3 rotation;
- Vector3 lastHit;
- // Use this for initialization
- void Start () {
- GroundObject = null;
- onGround = false;
- lastHit = new Vector3(0, 0, -100);
- }
- void OnCollisionEnter(Collision collision){
- }
- // Update is called once per frame
- void FixedUpdate () {
- speed = new Vector3(3, 0, 0);
- int x = Screen.width / 2;
- int y = Screen.height / 2;
- Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y));
- RaycastHit hit;
- Transform cam = Camera.main.transform;
- float snap = 15;
- // Release object when pressing left mouse button
- if(GroundObject != null && Input.GetMouseButtonDown(0)){
- GroundObject.layer = 0;
- GroundObject.rigidbody.isKinematic = false;
- GroundObject.rigidbody.mass = originalMass;
- GroundObject.rigidbody.freezeRotation = false;
- GroundObject = null;
- }
- // Get GameObject when pressing right mouse button
- if(GroundObject == null){
- if(Physics.Raycast(ray, out hit) && hit.distance < 5 && hit.transform.gameObject.rigidbody != null && Input.GetMouseButtonDown(1)){
- GroundObject = hit.transform.gameObject;
- originalMass = GroundObject.rigidbody.mass;
- GroundObject.rigidbody.mass = zero;
- GroundObject.layer = 2;
- GroundObject.rigidbody.rotation = Quaternion.identity;
- GroundObject.rigidbody.freezeRotation = true;
- GroundObject.rigidbody.isKinematic = true;
- }
- }
- if(GroundObject != null){
- if(GroundObject.rigidbody.isKinematic == false){
- GroundObject.rigidbody.velocity = Vector3.zero;
- }
- // Calculate GroundObjects position when on ground
- if (Physics.Raycast(ray, out hit) && hit.distance < 5) {
- GridController grid = (GridController) GameObject.Find("GridController").GetComponent(typeof(GridController));
- if (lastHit != hit.point){
- grid.Generate(hit.point);
- }
- lastHit = hit.point;
- GroundObject.rigidbody.MovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);
- // grid.Clear();
- onGround = true;
- }else{
- onGround = false;
- }
- // Rotate object with mouse when right mouse button is down
- if(Input.GetMouseButton(1)){
- if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
- {
- Vector3 roundAngles = new Vector3(Input.GetAxis("Mouse Y") * 2, Input.GetAxis("Mouse X") * 2, 0);
- roundAngles.x = Mathf.Round(roundAngles.x / snap) * snap;
- roundAngles.y = Mathf.Round(roundAngles.y / snap) * snap;
- roundAngles.z = Mathf.Round(roundAngles.z / snap) * snap;
- GroundObject.transform.Rotate(roundAngles, Space.World);
- }
- }
- if(GroundObject.transform.rotation != Quaternion.identity){
- Vector3 rotation = GroundObject.transform.eulerAngles;
- rotation.x = Mathf.Round(rotation.x / snap) * snap;
- rotation.y = Mathf.Round(rotation.y / snap) * snap;
- rotation.z = Mathf.Round(rotation.z / snap) * snap;
- GroundObject.transform.eulerAngles = rotation;
- }
- }
- }
- void LateUpdate () {
- if(GroundObject != null){
- // Set object in coordinates set in editor
- if(onGround == false){
- foreach (Transform child in transform)
- {
- if(child.name == "BuildObject_1"){
- GroundObject.rigidbody.isKinematic = true;
- GroundObject.transform.position = child.transform.position;
- }
- }
- }
- // Snapping coordinates
- if (onGround == true){
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement