Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by Clave · May 01, 2010 at 12:23 PM · instantiateinfrontdigging

Instantiating exactly above the ground

hello everyone!

Im trying to simulate digging on the ground, the player has a 'digging' animation, then a soil prefab with a texture of dirt will instantiate in front of him, this soil will grow every second to make it look that the dirt is piling up.

so far this it what i have, the soil has a rigid body so it will drop on the ground, im having some problem because sometimes, that soil will go through under the terrain

old script

//player.js function dig () { Instantiate (Soil, (transform.position + (transform.forward * 1.1)) +(transform.up * -1), Quaternion.identity); }

//soil.js function grow() { for ( i = 0 ; i <= 10 ; i++ ) { transform.localScale.x += 0.1; transform.localScale.z += 0.1; yield WaitForSeconds(0.5); } Destroy (rigidbody); Destroy (this); }

function Awake () { grow(); }

new script

//player.js

static var hitpass : Vector3; //this will be use on soil.js for the surface normal function dig () { var hit : RaycastHit; var ground = transform.TransformDirection(-Vector3.up);

 if(Physics.Raycast(transform.position, ground , hit, 10))
 {
     hitpass = hit.normal;
     Instantiate (Soil, hit.point, Quaternion.identity);

 }

}

//soil.js function grow() { for ( i = 0 ; i <= 10 ; i++ ) { transform.localScale.x += 0.1; transform.localScale.z += 0.1; yield WaitForSeconds(0.5); } Destroy (this); }

function Awake () { transform.up = player.hitpass; //passes the normal during the raycast from player.js grow(); }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by spinaljack · May 02, 2010 at 10:11 AM

Instead of giving the soil a rigid body you can use raycast to find exactly the point on the surface you want the soil to appear at.

When your character is digging, do a raycast a small distance in front of him pointing down, use the ray cast hit point data to instantiate the soil. You can even find the incline of the terrain and spawn the soil at the proper angle.

Use the normal of the surface as the up direction of your soil

EDIT

A method of changing a vector to a Quaternion is with the set look rotation function. The you stick the quaternion in the last argument of the instantiate function. You may need to rotate your soil model to face upwards.

Comment
Add comment · Show 4 · 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 Clave · May 03, 2010 at 07:02 PM 0
Share

hello, i successfully simulated the digging using a raycast, i updated the script above. i'm really interested at this "You can even find the incline of the terrain and spawn the soil at the proper angle." can you please tell me more about it? how can i get the rotation of the hit.point? i've tried different angle variables, nothing works so far.

avatar image spinaljack · May 03, 2010 at 11:18 PM 0
Share

You want the normal of the surface

avatar image Clave · May 04, 2010 at 12:05 AM 0
Share

it is now working perfectly! thank you very much. . but! :) i had to pass the raycast.normal data to the soil's script, do you think its necessary? is there a way to apply the raycast.normal during instantiating? i updated the code above.

avatar image spinaljack · May 04, 2010 at 12:21 AM 0
Share

You need to convert the vector into a Quaternion and then plug it into the instantiate function if you didn't want to pass any data

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

No one has followed this question yet.

Related Questions

Checking if object intersects? 1 Answer

instantiate objects in menu 1 Answer

Script that allows me to spawn enemies in game. 1 Answer

Position in front of an object? 2 Answers

Found if instantiate position already occupied 0 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