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 Toggy11 · Jan 12, 2014 at 11:09 AM · stay in place

Raycast Trouble

Okay I am trying to make this voxel based game (Not a minecraft clone its a fast-paced combat game). Though I just want to add this small part to the game which is breaking and placing cubes. Yep! That works fine but when I look down at my feet and I right-click (Break) I can destroy my player also I have to move far back to place a block or it will place it where my player is.

All I have is a camera (Break script is here) and a pickaxe parented to the camera so it looks like I'm breaking blocks with a tool.

Heres my break script:

     #pragma strict
     
         var blockLayer : LayerMask = 1;
         var range : float = Mathf.Infinity;
         var hit : RaycastHit;
          
         function Update () {
         if (Input.GetMouseButtonDown(1)) Build();
         if (Input.GetMouseButtonDown(0)) Erase();
         }
          
         function Build() {
         if (HitBlock()) {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = hit.transform.position + hit.normal;
         }
         }
          
         function Erase() {
         if (HitBlock())
         Destroy(hit.transform.gameObject);
         }
          
         function HitBlock() : boolean {
         return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
         }
 

Thanks in advance!

Comment
Add comment · Show 1
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 robertbu · Jan 12, 2014 at 11:19 AM 0
Share

I'm not sure what you are asking us to help you with. Are you asking for help because your character is being destroyed? If so, are you sure that your character is not on the block layer? If you look in the inspector, are you sure that the 'blockLayer' is set to only the block layer? Layer 1 (your initialization of 'blockLayer'). Is the 'Default' layer. Note primitive cubes are not Voxels.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by morbidcamel · Jan 12, 2014 at 11:39 AM

Hi this might help - use the Input.mousposition

         return Raycast(Input.mousePosition, out info);
     }
     
     private bool Raycast (Vector3 screenPoint, out RaycastInfo info) 
     {
         Ray ray = camera.ScreenPointToRay (screenPoint);
         
         return PhysicsUtil.Raycast(ray, out info, raycastDistance);
     }
 
Comment
Add comment · Show 3 · Share
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 Toggy11 · Jan 12, 2014 at 05:17 PM 0
Share

Could you explain that to me?

avatar image morbidcamel · Jan 12, 2014 at 10:51 PM 0
Share

Use the scene camera and then use ScreenPointToRay to create a Ray instance to pass to the ray cast. It means it will cast a ray from the mouse position into world space and is the most accurate way to obtain a Raycast from where you pointing at...

avatar image morbidcamel · Jan 12, 2014 at 10:53 PM 0
Share

Sorry if you confused with PhysicsUtil that is just a wrapper class I wrote to first test some common collider before doing a Physics,Raycast on every collider in the scene.

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

19 People are following this question.

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

Related Questions

No-Break/Non-Breaking Space in Strings 3 Answers

How to make mecanim animation stay at player's position 3 Answers

Dual Slider / Min Max Slider for GUI 2 Answers

My gun wont stay in the same spot 2 Answers

How can I draw the distance around a prefab? 1 Answer


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