Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 talibob · Aug 01, 2011 at 03:55 PM · instantiateminecraft

Creating blocks script! Need help :)

Hey guys. im trying to make a minecraft likely game, and im totally new to this. i never did JavaScript before but now im trying :) I made a block prefab witch is 4 cubes stick together. alt text

Okay so i tried me best to made a script witch is gonna made the cube im holding mouse over gray and when i click on it there is gonna be one more of these boxes. But when i click there comes a new box but it randomly spawns in the air :S

can anyone tell me what im doing wrong? :)

var Cube : Transform; function OnMouseOver() { renderer.material.color = Color.gray; if (Input.GetButtonDown("Fire1")) { Instantiate(Cube, transform.position, transform.rotation); } }

I was also trying to make a little spawn cube where the other box could spawn but im not sure how to do it :)

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 DayyanSisson · Aug 19, 2011 at 10:50 PM 0
Share

First of all, when you write code in your question, use the coding button so people can see it more clearly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DayyanSisson · Aug 02, 2011 at 03:46 AM

Well here is a script where by clicking, the object is created. This is part of the answer to your question.

 var newObject : Transform;
 
 function Update () { 
     if (Input.GetButtonDown("Fire1")) {
         Instantiate(newObject, transform.position, transform.rotation);
     }
 }

As for making the spawn cube, make a cube, and make sure that it is not touching any other objects. Then name the cube "spawnCube." On your script, write Find("spawnCube"). Also add the spawnCube variable. Now the cube should spawn on the spawnCube. Oh, and turn off the mesh renderer on the spawn cube to make it invisible and so it doesn't interfere with objects.

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 DeathByWarrior · Aug 02, 2011 at 04:59 AM

Maybe this will help (snaps cubes to a grid):

 var blockPrefab : GameObject; //Your block prefab
 var hit : RaycastHit; //Info about where and what the raycast hit
 var range : float = 5; //Max range of the raycast
 var blockLayer : LayerMask = 1; //Put your block in a layer and select that layer here (make sure its not the same as the player)

 function Update () {
     if (Input.GetMouseButtonDown(1))
         PlaceBlock();
     if (Input.GetMouseButtonDown(0))
         DestroyBlock();
 }

 function PlaceBlock() {
     if (HitBlock()) {
           var cube = Instantiate(blockPrefab ,hit.transform.position + hit.normal, Quaternion.identity); 
     }
 }

 function DestroyBlock(){
     if(HitBlock()){
         Destroy(hit.transform.gameObject);
     }
 }

 function HitBlock() : boolean{
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Place cubes? problem with my script :S 1 Answer

Instantiate A Prefab 1 Answer

Create a ball then launch it 3 Answers

Instantiated object don't recognize MouseClick. 0 Answers

This script does not work propely in my game 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