Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SphereCastTest : MonoBehaviour {
- public float radiusOfSphere, distance;
- Rigidbody selectedRigidbody;
- public GameObject rayOrigin;
- public LayerMask layerMask;
- void Update () {
- Vector3 origin = rayOrigin.transform.position;
- Vector3 direction = rayOrigin.transform.forward;
- if(Input.GetButtonDown("Fire1")) {
- RaycastHit hit;
- if (Physics.SphereCast(origin, radiusOfSphere, direction, out hit, distance, layerMask))
- {
- if (hit.collider.gameObject.tag == "Pickable")
- {
- selectedRigidbody = hit.rigidbody;
- selectedRigidbody.constraints =
- RigidbodyConstraints.FreezePositionZ
- | RigidbodyConstraints.FreezeRotationX
- | RigidbodyConstraints.FreezeRotationY
- | RigidbodyConstraints.FreezeRotationZ;
- }
- }
- }
- if (Input.GetButtonUp("Fire1"))
- {
- if (selectedRigidbody != null)
- {
- selectedRigidbody.constraints = RigidbodyConstraints.None;
- selectedRigidbody = null;
- }
- }
- }
- void OnDrawGizmos() {
- Vector3 origin = rayOrigin.transform.position;
- Vector3 direction = rayOrigin.transform.forward;
- Color color1 = new Color(1f, 1f, 1f, 0.5f);
- Color color2 = new Color(0f, 0f, 0f, 0.5f);
- for (float t = 0, step = 0.05f; t < 1f; t += step)
- {
- Gizmos.color = Color.Lerp(color1, color2, t);
- Vector3 pos = Vector3.Lerp(origin, origin + (direction*distance), t);
- Gizmos.DrawSphere(pos, radiusOfSphere);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment