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 RickyCharles · Apr 08, 2013 at 02:12 PM · playerobjectlocationrelativeplace

Place object relative to player location

Firstly I'd just like to say that this community so far has been invaluable to me!

Secondly the question, I'm trying to place an object (at the moment a cube) into the world via a keypress. I know the key press is all functional and everything, and I can place the object fine. I'm just not sure how to transform the object location to say 4 blocks infront of the player. I've been reading the transform.position help and to be honest I'm just not sure how to apply it.

Just to give as much info as I can, the script for this key input is on the player object which is called surprisingly "player". At the moment the cube is placed directly on top of the player. I can see that you can create objects at certain locations but I'm just not sure how to get the player's location, store that as a variable, then do object location = player.x + 4 in a sense.

Thanks! :)

     private string message = "";
      public Transform building;
  
 void Update()
 {
  if (Input.GetKeyDown("h")) // You must use 'Input' only at the 'Update' functions
  {
     if(variablesResources.GetWood() >= 3)
     {
       message = "YOU BUILT A HOUSE";
       variablesResources.SetWood(-3);
 
         Invoke("HideMessage", 3);
         var house = Instantiate(building, transform.position, transform.rotation);  
     }
     else
     {
          message = "YOU DONT HAVE ENOUGH WOOD";
                         Invoke("HideMessage", 3);
 
     }
   }
 }
     void HideMessage()
     {
         message = "";
     }
 
 void OnGUI() // Is not static and you should not be making it so
 {
     GUI.Label(new Rect(50, 50, 300, 20), message);
     
     }
 }
  
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 sdgd · Apr 08, 2013 at 04:32 PM 0
Share

wow does this even make a building?

as it's

public Transform building;

if not change Transform in to GameObject

well I don't know if it does but I use GameObject for this kind of stuff

avatar image RickyCharles · Apr 08, 2013 at 04:36 PM 0
Share

jesus christ i just realised I've had a snipped of javascript and it works perfectly what is even with my life right now

1 Reply

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

Answer by whebert · Apr 08, 2013 at 04:59 PM

var is valid C#, since C# 3.0 I think.

Anyway, if you always want to instantiate the object some distance in front of your current location, this would do the trick (using 4 as distance).

 float distance = 4.0f;
         
 Instantiate(building, transform.position + transform.forward*distance, transform.rotation);
Comment
Add comment · Show 1 · 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 RickyCharles · Apr 08, 2013 at 05:05 PM 0
Share

worked just as I needed :D I should probably start using variables more often so I'll use that float definitely

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

13 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

Related Questions

Changing Mesh and Mesh Collider of an Object 0 Answers

Make object visible only for camera "X" 1 Answer

Location and rotation relative to another location and rotation 0 Answers

How to have the Player place GameObjects in scene at runtime? 3 Answers

Having a particle effect stick to player 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