Created
February 17, 2020 14:50
-
-
Save sasame/55dd91ac94376b6475711d2664be9c44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class ScaleDrag : MonoBehaviour | |
| { | |
| Camera _cam; | |
| Transform _dragObject; | |
| Vector3 _localPos; | |
| Quaternion _localRot; | |
| Vector3 _localScale; | |
| float? _lenToWall; | |
| Vector3 _prevMouse; | |
| Vector3 _downMouse; | |
| private void Awake() | |
| { | |
| _cam = GetComponent<Camera>(); | |
| } | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (Input.GetKey(KeyCode.W)) | |
| { | |
| _cam.transform.position += _cam.transform.forward * Time.deltaTime; | |
| } | |
| if (Input.GetKey(KeyCode.S)) | |
| { | |
| _cam.transform.position -= _cam.transform.forward * Time.deltaTime; | |
| } | |
| if (Input.GetKey(KeyCode.A)) | |
| { | |
| _cam.transform.position -= _cam.transform.right * Time.deltaTime; | |
| } | |
| if (Input.GetKey(KeyCode.D)) | |
| { | |
| _cam.transform.position += _cam.transform.right * Time.deltaTime; | |
| } | |
| Vector3 mousePos = Input.mousePosition; | |
| mousePos.z = 10f; | |
| var ray = _cam.ScreenPointToRay(mousePos); | |
| var prevRay = _cam.ScreenPointToRay(_prevMouse); | |
| var downRay = _cam.ScreenPointToRay(_downMouse); | |
| if (Input.GetMouseButtonDown(0)) | |
| { | |
| _downMouse = mousePos; | |
| _dragObject = null; | |
| RaycastHit hit; | |
| if (Physics.Raycast(ray, out hit)) | |
| { | |
| if (hit.collider is SphereCollider) | |
| { | |
| _dragObject = hit.transform; | |
| _localPos = _cam.transform.worldToLocalMatrix.MultiplyPoint(_dragObject.position); | |
| _localRot = Quaternion.Inverse(_cam.transform.rotation) * _dragObject.rotation; | |
| _localScale = _dragObject.localScale; | |
| _dragObject.GetComponent<SphereCollider>().enabled = false; | |
| if (Physics.Raycast(ray, out hit)) | |
| { | |
| _lenToWall = hit.distance; | |
| }else{ | |
| _lenToWall = null; | |
| } | |
| } | |
| } | |
| }else if (Input.GetMouseButton(0)) | |
| { | |
| float dot = Vector3.Dot(prevRay.direction, ray.direction); | |
| if (dot < 1f) | |
| { | |
| Vector3 axis = Vector3.Cross(prevRay.direction, ray.direction).normalized; | |
| var rot = Quaternion.AngleAxis(Mathf.Acos(dot) * Mathf.Rad2Deg * 2f, -axis); | |
| _cam.transform.rotation = rot * _cam.transform.rotation; | |
| } | |
| if (_dragObject!=null) | |
| { | |
| _dragObject.position = _cam.transform.localToWorldMatrix.MultiplyPoint(_localPos); | |
| _dragObject.rotation = _cam.transform.rotation * _localRot; | |
| if (_lenToWall != null) | |
| { | |
| RaycastHit hit; | |
| if (Physics.Raycast(downRay, out hit)) | |
| { | |
| float distScale = hit.distance / _lenToWall.Value; | |
| Vector3 toObj = _dragObject.position - _cam.transform.position; | |
| _dragObject.localScale = _localScale * distScale; | |
| toObj *= distScale; | |
| _dragObject.position = _cam.transform.position + toObj; | |
| } | |
| } | |
| } | |
| } | |
| else if (Input.GetMouseButtonUp(0)) | |
| { | |
| if (_dragObject != null) | |
| { | |
| _dragObject.GetComponent<SphereCollider>().enabled = true; | |
| if (_dragObject.GetComponent<Rigidbody>() == null) | |
| { | |
| _dragObject.gameObject.AddComponent<Rigidbody>().useGravity = true; | |
| } | |
| _dragObject = null; | |
| } | |
| } | |
| _prevMouse = mousePos; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
錯覚を利用したオブジェクトスケールのテスト。