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 williampigmeu · Aug 01, 2012 at 10:41 PM · terrainmouseraycastingdeform

Please, I need help with Raycasting!

What i'm trying to do is get the mouse position, and do this: If mouse position (FPS crosshair) is in the terrain, if I click left mouse button, it makes a little "hole" in the terrain, not a real hole, just, make a crate, a small crate.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kag359six · Aug 01, 2012 at 11:23 PM

well for the raycast thing i would do this:

 var hit : RaycastHit; //collision detection variable

 if(Physics.RayCast(transform.position, transform.forward, hit, 10) {  //create ray

if(hit.collider.gameObject.tag == "terrain") { //if collider we aim at has tag terrain

if(Input.GetMouseButtonDown(0)) { //if we hit the left mouse button

//do some action

}

}

}

But I'm not sure about the terrain thing. To me it sounds like a complicated task since you are editing geometry during gameplay, and I don't see how you can accomplish that without some knowledge on modifying meshes through code. Or maybe there's some way to access the terrain tools..?

Comment
Add comment · 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
0

Answer by Ingen · Aug 01, 2012 at 11:00 PM

hi I think this can help you

  // put this in the camera,  
  
   var Range : float =  2; // distance of the ray from player, don't want the player piercing anywhere
  
  public var ClicktAudio : AudioClip;
  
 function Update (){
        var  Hit : RaycastHit;
        var DirectionRay = transform.TransformDirection(Vector3.forward * Range);
         
      // Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
    if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
      {
         Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
 
           if(Hit.collider.CompareTag ("terrain"))  
    {
           
              if(/*Input.GetButtonDown ("Fire1")*/  Input.GetKeyDown(KeyCode.R))// or any key you wont
                {
                         //DO THE HOLE   
                 }
                 }
           } 
 }  
    
Comment
Add comment · Show 8 · 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 williampigmeu · Aug 01, 2012 at 11:03 PM 0
Share

Well, i need some "//DO THE HOLE" code too. =/

avatar image williampigmeu · Aug 01, 2012 at 11:07 PM 0
Share

Is just to make a small crate at the crosshair position.

avatar image Ingen · Aug 01, 2012 at 11:23 PM 0
Share

you want a hole

or a crate

avatar image williampigmeu · Aug 01, 2012 at 11:42 PM 0
Share

Umm sorry, wrong word, not a crate, i want to make it dig a hole, yes, a hole, sorry, but a small hole.

avatar image Ingen · Aug 01, 2012 at 11:47 PM 0
Share

Ha! ha! :) no problem,

I'm also interest in to, take a look at this

http://answers.unity3d.com/questions/294572/modify-terrain-by-player.html

if some one answer

but maybe you can instantiate something like a hole mole on the terrain,

Show more comments
avatar image
0

Answer by midomido · Aug 01, 2012 at 11:09 PM

 function Update () {
     if (Input.GetButtonDown ("Fire1")) {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit)) {
             Instantiate(crate, hit.point, Quaternion.identity);
           //hit.point contain vector3 of collision location.     
 
         }
     }
 }
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 williampigmeu · Aug 01, 2012 at 11:22 PM 0
Share

Instantiate "crate"? It will make a crate?

avatar image kag359six · Aug 02, 2012 at 05:38 PM 0
Share

no, crate is a prefab in this case. You are saying crate, but dont you mean a hole? You are confusing everybody by saying crate lol

avatar image williampigmeu · Aug 02, 2012 at 06:41 PM 0
Share

Itza hole. Sry.

avatar image
0

Answer by kag359six · Aug 02, 2012 at 06:35 PM

You are trying to do something that's not so simple, but I found this for you:

Here's an article on modifying terrain heights, perhaps a hole as you say

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

When editing Terrain brush does not fall on cursor 1 Answer

Make a simple tree 1 Answer

Mouse Aim for a 2.5D game 1 Answer

Character Customization Problem, Hard to explain... JS 0 Answers

How To make a digging system 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