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
2
Question by Staude · Oct 21, 2011 at 01:08 PM · raycastpositionobjectmouse

Having an object point towards mouse position in 3d world

Hello. I've been trying to get this to work for.. Let's just say many hours now, and despite finding a bunch of suggestions and help threads i've been unable to solve it. So in a last attempt i'm writing here, hoping someone will be willing to help me find the solution.

I'm trying to have a pistol point at the mouse. It's fairly simple in theory.

I've tried using raycast to solve this by creating a gameobject i wanted to follow the mouse, but the problem with this is, that when i use transform.position, unity tells me that i'm unable to use vector3 as transform position.

While i've been able to get the information from the damn mouse, i'm unable to figure out a way to actually tell the object to move to the location.

So the big issue that i'm having is what to put inside the if statement. I realize that i've stored raycast in my variable, but i have no idea how to translate the contents of the variable to a float or a int or anything that i can use in combination with transform.position/translate or some other function that allows me to move my object to the mouses position.

A different thing i tried was the "lookAt", but since the mouse is on a 2d axis, it didn't really work.

Thanks in advance for the time.

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
2

Answer by syclamoth · Oct 21, 2011 at 01:38 PM

What you need is a combination between two things-

  1. A flat plane, or ground collider

  2. A raycast from the screen.

Now, as I understand it, you know how to get the mouse position. Now, use

 Camera.main.ScreenpointToRay(mousePosition);

to get a ray, then use Physics.Raycast against the ground collider to get the point.

One thing to remember is that using this method will cause the character to look slightly at the ground, more so if the mouse is close to them. To get around this, modify the value of the returned point from the raycast by the player's position on your 'up' axis, something like this-

 point = new Vector3(point.x, player.transform.position.y, point.z);

Then when you use LookAt, the player will always look around in a circle, not looking down at the ground.

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 Staude · Oct 21, 2011 at 02:35 PM 0
Share

Yes, that sounds right. The problem is, I am unable to translate it from theory to actual code. Not that I don't want to. What i currently have is this:

var pointPlace : GameObject;

function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast()){

point = new Vector3(pointPlace.transform.position.x, point.y, point.z);

pointPlace being the gameobject the script is also attached to. I've tried to get this work for about 10 hours now, so it's probably right in front of me. The whole problem that I have is that I can't translate the Input.mousePosition to an actual location i tell the object to go to. Ins$$anonymous$$d i say that heres the mouse position, if raycast hits then.. Do this. The problem being that "do this", does somehow not translate to "move my object". Thanks again for the time. I'm pretty new to scripting but I really want to learn it to a point where the logics of coding - and everything i code, make full sense to me :P

avatar image syclamoth · Oct 22, 2011 at 12:35 AM 0
Share

Oh, you're not using Raycast properly. it should be more like this-

 var hit : RaycastHit;

 if(Physics.Raycast(ray, hit))
 {
     var turnTowardsThis = new Vector3(hit.point.x, transform.position.y, hit.point.z);
     transform.LookAt(turnTowardsThis);
 }

The problem is twofold- firstly, you can't use raycast without any parameters (physics doesn't know where to put the ray), secondly you need to have a RaycastHit to get information back out of the raycast! After that, you're all good.

Of course, you still need a 'ground' object, so that your raycast has something to hit. You might even want to put a layermask on the raycast, so that it only hits objects marked as 'ground'! See the documentation for more information.

avatar image Staude · Oct 22, 2011 at 05:38 PM 0
Share

Thanks a lot for the assist. I really appreciate it. If I could vote the answer up twice i would. :)

And yeah, it works now. I didn't realize that the position had to be defined within the statement. I think I have the concept mostly down now. turnTowards this is the variable that stores the location specified and then the transform lookat allows you to rotate to look at that location. Again, thanks. I was pretty lost before :P

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fire weapon from turret to mouse position (3D Isometric shooter) 1 Answer

Mouse Click and Spawn object 4 Answers

Spawn Objects Where i click 2 Answers

Getting the position of an object's specific tri 1 Answer

Find point between object and mouse position 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