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 Hybris · Jan 06, 2012 at 09:02 PM · objectmousemovingtower

Dragging Objects with the mouse

Hello,

I want to be able to move a Object with the mouse, like in a TowerDefense game adding turrets. I am just learning javascript so I dont know too much, The API is great, but I cant let it work, this is my current code: var turret : Rigidbody;

function Update () {

turret.transform.position = Input.mousePosition;

}

function OnMouseDown () {

var placement : Rigidbody;

placement = Instantiate (turret, transform.position, transform.rotation);

}

}

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
1
Best Answer

Answer by adrenak · Jan 07, 2012 at 06:42 PM

Why dont you try something like this? :

 var turret : GameObject;   
 var viewHit : RaycastHit;
     
 function Update(){
             
     if(Input.GetButtonDown("Fire1"){
         if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), viewHit, 100)){  //100 is the range
                 Instantiate(turret, viewHit.point, Quaternion.identity);// instantiate the turret at the point where the raycast hits
         }
     }
 }// update ends

 

This code should go to your camera that you use, the game object 'turret' can have a rigidbody component. Hope this helps! Tell me if it doesnt I am not on my comp right now so not 100% sure :)

Comment
Add comment · Show 3 · 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 Hybris · Jan 08, 2012 at 10:12 AM 0
Share

right now it is just casting a ray from the camera down, but you really helped me, I just have to use something like $$anonymous$$ouseCursor.transform.position or something like that. I will have to consult the API(or script reference)

if it is casting from the mouse, then my mouse position is 800 meters in the air

avatar image syclamoth · Jan 08, 2012 at 10:52 AM 1
Share

Well, no- your mouse position is on the screen, not any actual point in 3d space! You need to use

 Camera.main.ScreenPointToRay(Input.$$anonymous$$ousePosition);

to get the correct ray.

avatar image adrenak · Jan 09, 2012 at 04:39 AM 0
Share

Yeah in the code that you have written there, turret.transform.position is a position vector in 3D space and Input.mousePosition is position in the screen co ordinates, you cant equate them directly.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving a object around with mouse, rotating it with arrow keys and placing it. 1 Answer

Detect a click outside a GUI/object 4 Answers

Object dissapears when moving fast 2 Answers

Animate an object 1 Answer

Animated Object moving when clicked 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