Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cj31387 · Aug 10, 2012 at 08:21 AM · raycastmousetargetlock

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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cj31387 · Aug 11, 2012 at 04:26 AM 0
Share

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.

avatar image Seth-Bergman · Aug 11, 2012 at 11:28 PM 1
Share

you might try this:

http://docs.unity3d.com/Documentation/ScriptReference/Screen-lockCursor.html

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast point to provive a target not working? 1 Answer

How to make a line renderer (laser beam) smoothly follow the mouse? 1 Answer

Turn in the direction of movement 0 Answers

Mouse plane does not detect height 1 Answer

OnMouseExit Problems vs. RaycastAll 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges