Created
May 20, 2020 06:13
-
-
Save igrir/dc76449c35ba3f9d1f93589dcc3deae1 to your computer and use it in GitHub Desktop.
Si script gerakan relatif kubus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering.UI; | |
public class BoxGizmo : MonoBehaviour | |
{ | |
public Vector3 rightPlaneWeight = new Vector3(); | |
public Vector3 upPlaneWeight = new Vector3(); | |
public Vector3 forwardPlaneWeight = new Vector3(); | |
public Vector3 maxAbsResult = new Vector3(); | |
public bool moveX; | |
public bool moveY; | |
public bool moveZ; | |
Vector3 startXPlanePos = Vector3.zero; | |
Vector3 currentXPlanePos = Vector3.zero; | |
Vector3 startYPlanePos = Vector3.zero; | |
Vector3 currentYPlanePos = Vector3.zero; | |
Vector3 startZPlanePos = Vector3.zero; | |
Vector3 currentZPlanePos = Vector3.zero; | |
Vector3 startRightPlanePos = Vector3.zero; | |
Vector3 currentRightPlanePos = Vector3.zero; | |
Vector3 startUpPlanePos = Vector3.zero; | |
Vector3 currentUpPlanePos = Vector3.zero; | |
Vector3 startForwardPlanePos = Vector3.zero; | |
Vector3 currentForwardPlanePos = Vector3.zero; | |
public float xDistance; | |
public float yDistance; | |
public float zDistance; | |
private bool moved = false; | |
private bool movedUpdateMouseInit; | |
public float forwardDot; | |
public float upDot; | |
public float rightDot; | |
[Header("Config")] | |
public float planeTolerance = 0.7f; | |
// Update is called once per frame | |
void Update() | |
{ | |
if(CubeTargeter.CurrentCube != this) | |
return; | |
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); | |
float enter; | |
Plane upPlane = new Plane(this.transform.up, this.transform.position); | |
Plane forwardPlane = new Plane(this.transform.forward, this.transform.position); | |
Plane rightPlane = new Plane(this.transform.right, this.transform.position); | |
if (upPlane.Raycast(mouseRay, out enter)) | |
{ | |
Vector3 targetPos = mouseRay.GetPoint(enter); | |
if (Input.GetMouseButtonDown(0) || (Input.GetMouseButton(0) && movedUpdateMouseInit)) | |
{ | |
startXPlanePos = targetPos; | |
// startYPlanePos = targetPos; | |
startZPlanePos = targetPos; | |
startUpPlanePos = targetPos; | |
} | |
if (Input.GetMouseButton(0)) | |
{ | |
currentXPlanePos = targetPos; | |
// currentYPlanePos = targetPos; | |
currentZPlanePos = targetPos; | |
currentUpPlanePos = targetPos; | |
} | |
upPlaneWeight.x = Vector3.Dot(this.transform.right, Vector3.Normalize(startXPlanePos - currentXPlanePos)); | |
upPlaneWeight.y = 0; | |
upPlaneWeight.z = Vector3.Dot(this.transform.forward, Vector3.Normalize(startZPlanePos - currentZPlanePos)); | |
} | |
if (forwardPlane.Raycast(mouseRay, out enter)) | |
{ | |
Vector3 targetPos = mouseRay.GetPoint(enter); | |
if (Input.GetMouseButtonDown(0) || (Input.GetMouseButton(0) && movedUpdateMouseInit)) | |
{ | |
startXPlanePos = targetPos; | |
startYPlanePos = targetPos; | |
// startZPlanePos = targetPos; | |
startForwardPlanePos = targetPos; | |
} | |
if (Input.GetMouseButton(0)) | |
{ | |
currentXPlanePos = targetPos; | |
currentYPlanePos = targetPos; | |
// currentZPlanePos = targetPos; | |
currentForwardPlanePos = targetPos; | |
} | |
forwardPlaneWeight.x = | |
Vector3.Dot(this.transform.right, Vector3.Normalize(startXPlanePos - currentXPlanePos)); | |
forwardPlaneWeight.y = Vector3.Dot(this.transform.up, Vector3.Normalize(startYPlanePos - currentYPlanePos)); | |
forwardPlaneWeight.z = 0; | |
} | |
if (rightPlane.Raycast(mouseRay, out enter)) | |
{ | |
Vector3 targetPos = mouseRay.GetPoint(enter); | |
if (Input.GetMouseButtonDown(0) || (Input.GetMouseButton(0) && movedUpdateMouseInit)) | |
{ | |
// startXPlanePos = targetPos; | |
startYPlanePos = targetPos; | |
startZPlanePos = targetPos; | |
startRightPlanePos = targetPos; | |
} | |
if (Input.GetMouseButton(0)) | |
{ | |
// currentXPlanePos = targetPos; | |
currentYPlanePos = targetPos; | |
currentZPlanePos = targetPos; | |
currentRightPlanePos = targetPos; | |
} | |
rightPlaneWeight.x = 0; | |
rightPlaneWeight.y = Vector3.Dot(this.transform.up, Vector3.Normalize(startYPlanePos - currentYPlanePos)); | |
rightPlaneWeight.z = | |
Vector3.Dot(this.transform.forward, Vector3.Normalize(startZPlanePos - currentZPlanePos)); | |
} | |
if (movedUpdateMouseInit) | |
{ | |
movedUpdateMouseInit = false; | |
} | |
maxAbsResult.x = Mathf.Max(Mathf.Abs(upPlaneWeight.x), Mathf.Abs(forwardPlaneWeight.x), | |
Mathf.Abs(rightPlaneWeight.x)); | |
maxAbsResult.y = Mathf.Max(Mathf.Abs(upPlaneWeight.y), Mathf.Abs(forwardPlaneWeight.y), | |
Mathf.Abs(rightPlaneWeight.y)); | |
maxAbsResult.z = Mathf.Max(Mathf.Abs(upPlaneWeight.z), Mathf.Abs(forwardPlaneWeight.z), | |
Mathf.Abs(rightPlaneWeight.z)); | |
var cameraForward = Camera.main.transform.forward; | |
//TODO: nearest plane: | |
// up : choose better X or Z position, but not Y | |
// forward : choose better X or Y, but not Z | |
// right: choose better Z or Y, but not X | |
upDot = Mathf.Abs(Vector3.Dot(this.transform.up, cameraForward)); | |
forwardDot = Math.Abs(Vector3.Dot(this.transform.forward, cameraForward)); | |
rightDot = Math.Abs(Vector3.Dot(this.transform.right, cameraForward)); | |
if (maxAbsResult.x > maxAbsResult.y && maxAbsResult.x > maxAbsResult.z) | |
{ | |
moveX = true; | |
} | |
if (maxAbsResult.y > maxAbsResult.x && maxAbsResult.y > maxAbsResult.z) | |
{ | |
moveY = true; | |
} | |
if (maxAbsResult.z > maxAbsResult.y && maxAbsResult.z > maxAbsResult.x) | |
{ | |
moveZ = true; | |
} | |
if (moveX && rightDot > planeTolerance) | |
{ | |
moveX = false; | |
moveY = true; | |
moveZ = true; | |
} | |
if (moveY && upDot > planeTolerance) | |
{ | |
moveY = false; | |
moveX = true; | |
moveZ = true; | |
} | |
if (moveZ && forwardDot > planeTolerance) | |
{ | |
moveZ = false; | |
moveX = true; | |
moveY = true; | |
} | |
if (moveX) | |
{ | |
// get most near plane | |
// x can be obtained from upPlane and forwardPlane | |
if (Mathf.Abs(Vector3.Dot(cameraForward, this.transform.up)) > Mathf.Abs(Vector3.Dot(cameraForward, this.transform.forward))) | |
{ | |
xDistance = currentUpPlanePos.x - this.transform.position.x; | |
} | |
else | |
{ | |
xDistance = currentForwardPlanePos.x - this.transform.position.x; | |
} | |
if (xDistance > 1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x + 1, this.transform.position.y, | |
this.transform.position.z); | |
moved = true; | |
} | |
else if (xDistance < -1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x - 1, this.transform.position.y, | |
this.transform.position.z); | |
moved = true; | |
} | |
} | |
if (moveY) | |
{ | |
// get most near plane | |
// y can be obtained from rightPlane and forwardPlane | |
if (Mathf.Abs(Vector3.Dot(cameraForward, this.transform.right)) > Mathf.Abs(Vector3.Dot(cameraForward, this.transform.forward))) | |
{ | |
yDistance = currentRightPlanePos.y - this.transform.position.y; | |
} | |
else | |
{ | |
yDistance = currentForwardPlanePos.y - this.transform.position.y; | |
} | |
if (yDistance > 1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + 1, | |
this.transform.position.z); | |
moved = true; | |
} | |
else if (yDistance < -1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y - 1, | |
this.transform.position.z); | |
moved = true; | |
} | |
} | |
if (moveZ) | |
{ | |
// get most near plane | |
// z can be obtained from upPlane and rightPlane | |
if (Mathf.Abs(Vector3.Dot(cameraForward, this.transform.up)) > Mathf.Abs(Vector3.Dot(cameraForward, this.transform.right))) | |
{ | |
zDistance = currentUpPlanePos.z - this.transform.position.z; | |
} | |
else | |
{ | |
zDistance = currentRightPlanePos.z - this.transform.position.z; | |
} | |
if (zDistance > 1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, | |
this.transform.position.z + 1); | |
moved = true; | |
} | |
else if (zDistance < -1) | |
{ | |
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, | |
this.transform.position.z - 1); | |
moved = true; | |
} | |
} | |
moveX = false; | |
moveY = false; | |
moveZ = false; | |
if (moved) | |
{ | |
movedUpdateMouseInit = true; | |
moved = false; | |
} | |
} | |
private void OnDrawGizmos() | |
{ | |
Gizmos.color = Color.red; | |
Gizmos.DrawLine(startUpPlanePos, currentUpPlanePos); | |
Gizmos.DrawSphere(currentUpPlanePos, 0.25f); | |
Gizmos.color = Color.green; | |
Gizmos.DrawLine(startRightPlanePos, currentRightPlanePos); | |
Gizmos.DrawSphere(currentRightPlanePos, 0.25f); | |
Gizmos.color = Color.blue; | |
Gizmos.DrawLine(startForwardPlanePos, currentForwardPlanePos); | |
Gizmos.DrawSphere(currentForwardPlanePos, 0.25f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment