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 ujellyx · Mar 25, 2013 at 08:24 PM · raycastspawnbuildingrts

Spawn referencable objects with RayCast

Hello, i'm new to Unity and the whole scripting-thingy and am currently working on a RTS-Game with the Fury Framework.

I'm looking for a way, to spawn referencable objects with a click on a built terrain.

What i currently use:

 public GameObject BuildingType;
 
 [...]
 
 PlaceBuilding(Input.mousePosition);
 
 [...]
 
 public void PlaceBuilding(Vector2 mousePosition)
 {
      RaycastHit hit = RayFromCamera(mousePosition, 1000.0f);
      GameObject.Instantiate(BuildingType, hit.point, Quaternion.identity);
 }
 
 public RaycastHit RayFromCamera(Vector3 mousePosition, float rayLength)
 {
      RaycastHit hit;
      Ray ray = Camera.main.ScreenPointToRay(mousePosition);
      Physics.Raycast(ray, out hit, rayLength);
      return hit;
 }

I experience two problems with this:

  1. The building spawns, but always spawns right in the middle of the screen/camera instead of where the mouse is. (I'm using custom-textured mouse-cursors)

  2. How can i add specific scripts to these newly spawned objects (i can only modify them when the game is running) and use them as individual objects?

Thanks in advance!

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
0

Answer by dorpeleg · Mar 26, 2013 at 12:25 AM

To raycast at mouse position, you ray should be like this:

 ray = Camera.main.ScreenPointToRay(Input.mousePosition);

For adding scripts to the objects: GameObject.AddComponent.

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 ujellyx · Mar 26, 2013 at 08:21 AM 0
Share

When i try to add scripts with AddComponent i get the Error, that an object reference is required to access non-static member "UnityEngine.Gameobject.AddComponent(string)". How do i get unique object references each time i spawn a given GameObject? Do i have to make a list of GameObjects in order to achieve this?

The fix for the raycast didn't work... i don't know why. Each time i press esc to cancel the game-mode i notice, that the mouse is automatically placed to the middle of the screen - maybe this has something to do with it. Think i have to check the GUI-Class, where the custom-mouse-cursor is handled.

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

11 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

Related Questions

Best solution for Grid-based building system? 0 Answers

I need help for my RTS Games! 0 Answers

Automatically rotating objects to fit on to others 0 Answers

Unity3D - RTS Game - Instantiate object on buttonclick and make it follow the cursors position 2 Answers

How to Place a Game Object in game(Place a building) 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