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 BeerHuntor1 · Apr 24, 2020 at 07:55 PM · vector3raycasthitmovetowards

Can Vector3.MoveTowards take a RaycastHit.point as a param?

I am trying to get a player to move towards my mouse cursor position. I am trying to figure out raycasting, and have managed to debug the global coordinates of my mouse position.. However I am struggling to figure out why my player is shooting up into the y direction.. At first i figured it was down to my camera position, as when i tried this before, the coords were static. So i specifically set a reference to my players position to see if it was that, which was the problem, but still flies up into the sky..

 public class MousePlayerController : MonoBehaviour
 {
 
     public float speed = 3f; 
     private Camera cam;
     private Vector3 playerPos;
     // Start is called before the first frame update
     void Start()
     {
         cam = Camera.main;
         playerPos = GameObject.Find("Player").transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         Ray camRay = cam.ScreenPointToRay(Input.mousePosition);
         //Plane rayPlane = new Plane(Vector3.up, Vector3.zero);
         RaycastHit rayLength;
        // mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        
         if(Physics.Raycast(camRay, out rayLength, Mathf.Infinity)) {
             Debug.Log(rayLength.point);
             transform.Translate(Vector3.MoveTowards(playerPos, rayLength.point , speed * Time.deltaTime));
         }
         
         //transform.Translate(Vector3.right * speed * Time.deltaTime);
     }
 }
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 jkpenner · Apr 24, 2020 at 10:06 PM

Yes you can use RaycastHit.point in Vector3.MoveTowards.RaycastHit.point is just a position in 3d space.

In your code, you're accidentally translating your player's transform by your desired position. You should be able just to set your player's position to the MoveToward's value.

 transform.position = Vector3.MoveTowards(playerPos, rayLength.point, speed * Time.deltaTime);
Comment
Add comment · Show 1 · 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 BeerHuntor1 · Apr 24, 2020 at 11:08 PM 0
Share

Thank you, I have since figured this out. I have been folliowing a course, that basically used translate for all movement, and got set in the ways of thinking I had to use that to move. And that somehow transform.position was instantaneous which is not what I was after... Thanks for the help.

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

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

How to move a child object by drag and drop? Please help me :( 0 Answers

If (transform.position == Vector3.MoveTowards (transform.position, Vector3.zero, Time.deltaTime)) {...} 2 Answers

Unity 5 C# - MoveTowards is only moving in a single direction 1 Answer

How to get a proper ramming effect? 1 Answer

How to move an object from another position using Vector3.moveTowards precisely and slowly by swiping (mobile) 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