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 Tea_Doogun · Jul 19, 2012 at 12:21 PM · mouseraycasthitcursortransform.translate

transform.Translate: object NOT moving toward RaycastHit problem

This is a test script i'm using to iron out bugs before i adapt it to use as a 'shoot toward mouse cursor' script.

Basicly, the script is on a cube (NO rigid body), and whenever the mouse cursor touches another object in the scene, the box is supposed to move toward the mouse cursor because the raycast is pointing to the cursor and the box is moving toward the raycast.

HOWEVER, the box moves in a seemingly random direction and NOT toward the mouse cursor (Grrr) Can anyone tell me where i've gone wrong?

function Update() {

var hit : RaycastHit; if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {

transform.Translate((hit.point) * Time.deltaTime);

}

Thank you, Tom :)

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

Answer by whydoidoit · Jul 19, 2012 at 12:36 PM

Transform.Translate needs a direction and not a location. So I guess this would do it:

      transform.Translate((hit.point - transform.position) * Time.deltaTime);
Comment
Add comment · Show 6 · 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 Tea_Doogun · Jul 19, 2012 at 12:40 PM 0
Share

That did it, AND i learned somthing :)

Thanks man, yoo rool!

$$anonymous$$ :)

avatar image Bunny83 · Jul 19, 2012 at 01:34 PM 1
Share

Space.World? ;)

avatar image whydoidoit · Jul 19, 2012 at 01:49 PM 1
Share

Good point. @Tea_Doogun - you should pass Space.World to Translate as if the object were parented it would probably not be moving in the right direction.

avatar image Bunny83 · Jul 19, 2012 at 10:01 PM 1
Share

@whydoidoit: It doesn't actually need to be parented. Rotated is enough ;) Translate, with the default parameter Space.Self, will translate along the objects local axis. Since the direction you pass is in worldspace it won't be moving the right way, unless the object isn't rotated.

avatar image whydoidoit · Jul 19, 2012 at 10:17 PM 1
Share

Ah yes - serves me right for commenting on a function I don't use :S

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

7 People are following this question.

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

Related Questions

Changing cursor over multiple objects 0 Answers

Help in GUI issue - Can't lose focus of a dropdown list when mouse clicks somewhere else? 0 Answers

How do I make my mouse go back to default after clicking empty space? 0 Answers

unity2D: make object face mouse 2 Answers

Restrict the cursor to a window in Unity 5 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