Quantcast
Channel: Answers by "ransomink"
Viewing all articles
Browse latest Browse all 80

Answer by ransomink

$
0
0
The first problem I see is setting up your variables. You create float variables, then cast them as floats, that's unnecessary. You were likely getting an error because you left out the "f" after each value; all float values must have an "f" after the number. public float Axisz = (float)15.92; should be this: public float Axisz = 15.92f; - Next, I placed your GameObject and component variables at the top because they're easier to see and grab references as needed. I also changed how some variables were named *Axisz* to *axisZ* will read easier in the inspector and in code. - Initializing *RaycastHit* is unnecessary. Doing `RaycastHit hit; ` is fine. - Don't extend a ray's length more than it needs to. The greater the ray, the more objects you have to check against it, so using 1000 is overkill. - Print() is a wrapper for Debug.Log() and easier to type but only works on classes derived from MonoBehaviour. Because of this it's simpler to use Debug.Log() so you don't mix-and-match them depending on your class inheritance. - It's more optimal to cache your gameobject's position if used multiple times. private Transform t; // This gameobject's transform void Awake() { t = transform; } // Set to current position with offset selection.transform.position.Set( t.position.x + axisX, axisY, t.position.z + axisZ ); - The int value in CheckSphere() is not the layer id but a bitmask. If you want to hit an object on layer id 20, you must specify 1 << 20, not 20. if ( Physics.CheckSphere( spawnPos, radius, 20 ) ) needs to be if ( Physics.CheckSphere( spawnPos, radius, 1 << 20 ) ) Even easier, you can create a *LayerMask* variable and use that instead. public LayerMask markerLayer; // Marker layer if ( Physics.CheckSphere( spawnPos, radius, markerLayer ) ) - You should not constantly *Destroy* and *Instantiate* game objects. That'll ruin your performance. If you're only using one maker, simply activate and deactivate the game object. // Hit marker? if ( Physics.CheckSphere( spawnPos, radius, 1 << 20 ) ) { Debug.Log( "Clearing marker..." ); Dig.Play(); markerObject.SetActive( false ); } else { Debug.Log( "Placing marker..." ); Dig.Play(); // Reposition marker markerObject.SetActive( true ); markerObject.transform.position.Set( t.position.x, t.position.y + markY, t.position.z ); } - Always use CompareTag(), it does not allocate garbage. // Hit bedrock? if ( hit.transform.tag == "Bedrock" ) { selection.SetActive( false ); } into // Hit bedrock? if ( hit.collider.CompareTag( "Bedrock" ) ) { selection.SetActive( false ); } - Here is the final code refactored: using System.Collections; using System.Collections.Generic; using UnityEngine; public class DirtBlockHover : MonoBehaviour { public GameObject selection; public AudioSource dig; // Sound [Header("Positions")] public float axisX = -17.13f; // X Offset public float axisY = 24.1f; // Y Value public float axisZ = 15.92f; // Z Offset public float markY = 4.5f; // For the marker [Header("Marker Detection")] public GameObject marker; // Marker public LayerMask markerLayer; // Marker layer public float markerRadius; // Radius of sphere public Vector3 markerPosition; // Center of sphere private Transform t; // This gameobject's transform private GameObject markerObject; // Marker instantiated clone void Awake() { t = transform; } // Upon entering the dirtblock, gold, or Gems object which this script is attached to private void OnMouseEnter() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // Hit something? if ( Physics.Raycast( ray, out hit, 100 ) ) { Debug.Log( "You hit something!" ); selection.SetActive( true ); // Set to current position with offset selection.transform.position.Set( t.position.x + axisX, axisY, t.position.z + axisZ ); // Left click? if ( Input.GetMouseButtonDown( 0 ) ) { // Hit marker? if ( Physics.CheckSphere( markerPosition, markerRadius, markerLayer ) ) { Debug.Log( "Clearing marker..." ); dig.Play(); markerObject.SetActive( false ); } else { Debug.Log( "Placing marker..." ); dig.Play(); // Reposition marker markerObject.SetActive( true ); markerObject.transform.position.Set( t.position.x, t.position.y + markY, t.position.z ); } } } // Hit bedrock? if ( hit.collider.CompareTag( "Bedrock" ) ) { selection.SetActive( false ); } } private void OnMouseExit() { selection.SetActive( false ); } } - You don't have to comment every line of code. It's good so you don't forget what you did but naming variables and methods properly will help your code read itself. Playing an audio sound or debug.log doesn't need a comment. For code that merits commenting, make sure to say in as few words what the following code/line does...

Viewing all articles
Browse latest Browse all 80

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>