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 NutellaDaddy · May 06, 2014 at 12:22 AM · c#transformpositionvector3

Why does this placement script not work?

I am getting no errors or anything yet when I start up the game and click the button to instantiate a prefab that I can move around it doesn't work. The object doesn't even show up. Why is this. I am creating this script that makes the movement and instantiation of the object avalaible and than I am calling it in another script for when I click a button. I am trying to get it so that the gameobject moves relative to your objects position and it should always be 15 units away from the camera until this certain point which I will add to the script later. Can someone tell me whyt his isn't working. I have very basic knowledge on working with Vectorsa and such. Thank you for any help, here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class PlacingParts : MonoBehaviour 
 {
     private Transform currentPart;
 
     void Start () 
     {
     
     }
     void Update ()
     {
         if (currentPart != null)
         {
             Vector3 m =  Input.mousePosition;
 
             currentPart.position = new Vector3(m.x,m.y,15);
         }
     }
     public void PlaceItem(GameObject b)
     {
 
         currentPart = (((GameObject)Instantiate(b)).transform);
     }
 }



////////////////////////OTHER SCRIPT BELOW/////////////////////////////

 //This is where I am calling the placingObjects script
 //I stored the placingParts script in a variable and than //used it
 
         GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
                 for (int i = 0; i< cockpit.Length; i++)
                     {
                         if(GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),cockpit[i].name, partSlotButton))
                         {
                             placingparts.PlaceItem(cockpit[i]);
                         }
                     }
             GUI.EndGroup ();
 
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 Clet_ · May 06, 2014 at 12:27 AM 0
Share

The problem could come from your caller function. Would you $$anonymous$$d posting the code that calls the PlaceItem function?

avatar image NutellaDaddy · May 06, 2014 at 12:36 AM 0
Share

I posted it in the question. Thanks for any help you give!

1 Reply

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

Answer by Jeff-Kesselman · May 06, 2014 at 12:28 AM

Okay, so a few things.

The first is that the mouse X and Y are in screen pixel coordinates, not world coordinates.

If you want to know where that point is on the screen in world coordinates you need to use Camera.main.ScreenToWorldPoint(Input.mousePosition).

Next, I don't actually see PlaceItem being called at all.

Comment
Add comment · Show 10 · 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 NutellaDaddy · May 06, 2014 at 12:32 AM 0
Share

It's on a different script. I'll post it in the question.

avatar image NutellaDaddy · May 06, 2014 at 12:36 AM 0
Share

After I changed the Vector3 m to equal Camera.main.ScreenToWorldPoint(Input.mousePosition). It just instantiates the object off to the side and I can't move it around or anything. What would be the fix to this?

avatar image Clet_ · May 06, 2014 at 12:38 AM 0
Share

If you call

 Camera.main.ScreenToWorldPoint (Input.mousePosition)


it will always return a point on the camera's near clip. You need to specify the distance from the camera you want the point to be. The call would look like this

 Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15f));
avatar image NutellaDaddy · May 06, 2014 at 12:44 AM 0
Share

That works ,but it doesn't always stay in front of my camera when the camera rotates and it's slightly off from the mouse. Any solution to these problems?

avatar image Jeff-Kesselman · May 06, 2014 at 12:51 AM 0
Share

Well, I've had the best luck with exact positioning by using a Plane and a Plane ray cast.

can you tell us exactly what you are trying to accomplish?

Show more comments

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3 Transform.Position Not Working 2 Answers

What is wrong with my zipline script? 2 Answers

Player not facing the mouse correctly 1 Answer

Vector movement according to rotation glitching out at non-square angles. 0 Answers

Rotating object on one axis using Input.GetAxis 2 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