- Home /
Mouse lock to target
Hi, currently I'm working on a woodcutting crafting script. So far everything is working like auto chop and a progress bar, but when autochop is going mouse has to stay hovered over a tree or it stops and doesn't finish cutting down the tree until all the durability is gone. Is there any way to make the mouse cursor stay locked onto the tree after you click it? So far I have it so when you hover the mouse over a tree it raycasts then starts autochopping down the tree but I need to know how to make it lock to the tree. Thanks.
heres my code so far.
// Variables
public float woodAmount = 0f;
public float woodDurability = 0f;
public AudioClip woodSoundClip;
// Art for Progress bar
public Texture2D whiteTexture;
public Texture2D greyTexture;
// Variables for Progress Bar
public float curCount = 0;
public float maxCount = 100;
public bool barCounting = false;
// Variables for Lockon
public bool locked;
// Use this for initialization
void Start()
{
// The wood durability is a random range of 1-15
woodDurability = Random.Range(1, 15);
}
// Update is called once per frame
void Update()
{
if (barCounting == true)
{
ProgressBar();
}
if (Input.GetButtonDown("Fire1"))
{
AutoChop();
locked = true;
}
}
void OnGUI()
{
// Displays how much wood you have.
GUI.Box(new Rect(10, 430, 200, 30), "Wood: " + woodAmount);
// Displays a progress bar
if (barCounting == true)
{
GUI.DrawTexture(new Rect(0, 0, 200, 20), whiteTexture);
GUI.DrawTexture(new Rect(0, 0, 200 * (curCount / maxCount), 20), greyTexture);
}
}
// Function for progress bar execution.
void ProgressBar()
{
curCount++;
if (curCount == maxCount)
{
barCounting = false;
curCount = 0;
}
}
void AutoChop()
{
// A ray is cast from the camera to where the mouse cursor is, I will change this later
// to be based off the crosshair.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// This is the layer mask which in unity should be set to layer 11 for the tree you want wood from and labeled "Tree"
int layerMask = 1 << 11;
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask) && barCounting == false)
{
//target = hit.collider.transform;
//transform.LookAt(target);
// if locked enabled make the character stay locked
if (locked)
{
if (woodDurability > 0)
{
barCounting = true;
audio.PlayOneShot(woodSoundClip);
Debug.Log(hit.collider.gameObject.name);
// Adds 1 wood to the player.
woodAmount++;
// Takes away 1 wood from the tree untill 0 then the tree gets destroyed.
woodDurability--;
Invoke("AutoChop", 2);
if (woodDurability == 0)
{
Destroy(hit.collider.gameObject);
// Resets the wood durabilty again.
woodDurability = Random.Range(1, 15);
locked = false;
}
}
}
}
}
}
Comment
I could use a crosshair + the lookat function, ins$$anonymous$$d but the game is 3rd person and I haven't made the 3rd person controller yet. I would still need a way to lock on.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075448im_/https://answers.unity.com/themes/thub/images/avi.jpg)