Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Caldas10 · Oct 05, 2017 at 05:06 PM · movementlookattop down shooter

3D Top down movement. Move towards the mouse position.

Hello, im making a 3d top down game, and now my movement script make the player look at the mouse position in the map by a rayCast, and move around with the getAxis("Horizontal") and getAxis("Vertical"). Basically my problem its when i press W the player mover vertically, and i want him to move in the direction he is looking. Hope someone understand my question. Thanks.

             void MovementTopDown()
 {
     {
         moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
         moveVelocity = moveInput * moveSpeed;

         Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);

         Plane groundPlane = new Plane(Vector3.up, Vector3.zero);

         float rayLenght;

         if (groundPlane.Raycast(cameraRay, out rayLenght))
         {
             Vector3 pointToLook = cameraRay.GetPoint(rayLenght);
             Debug.DrawLine(cameraRay.origin, pointToLook, Color.red);

             transform.LookAt(pointToLook);
         }
     }

 }


     void FixedUpdate()
 {
     rb.velocity = moveVelocity;
 }
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 FortisVenaliter · Oct 05, 2017 at 06:57 PM

Okay, two things:

  1. You should never set the Rigidbody.velocity variable. You're basically overriding the physics system, so when doing this you shouldn't use a Rigidbody at all. Remove it.

  2. Your basic problem is that you need to convert your velocity from local space (forward is the entity's forward) to world space (forward is Z+ axis). To do this, you can use the Transform.TransformVector() function, then use that as the actual velocity.

Comment
Add comment · Show 4 · 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 Caldas10 · Oct 05, 2017 at 07:42 PM 0
Share

what does this function? what vector should a put Transform.TransformVector(HERE) ?

avatar image FortisVenaliter Caldas10 · Oct 05, 2017 at 07:46 PM 0
Share

That function will convert your velocity from local to world.

So, the parameter will be the velocity in local space (the one you're already calculating).

The output return value will be the velocity in world space (which you'll use to actually move your object).

avatar image Caldas10 · Oct 05, 2017 at 08:12 PM 0
Share

can you exemplify?

avatar image Caldas10 · Oct 05, 2017 at 09:07 PM 0
Share

OH i get it, its working. Thanks ;)

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

104 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 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 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 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 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

Quaternion Look at Target , i have trouble , pls help me !!!!! 1 Answer

Rotate character to the moving direction problems? 2 Answers

2D Topdown Character Contoller 0 Answers

Face target first then move forward to it - Top Down 2D 1 Answer

How do I get GameObjects to look at me? 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