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 Dharke · Aug 01, 2017 at 01:45 PM · raycastcontrollersinput.mouseposition

moving towards mouse position

Hi there, My game is using top down orthographic.. by holding the mouse button the player object moves towards the mouse position... Using Raycast i was able to successfully detect target location using the mouse position and able to move the character there, however, my character drifts upwards along the Y axis.. ive spent days googling and trying different methods and im sure it has something to do with the way the camera (ScreenPointToRay) calculates its own transform in 3D space.. please correct me if im wrong, but can someone also point me in the right direction ? thanks in advance :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Controller : MonoBehaviour
 {
 
     public Transform moveThis;
     public float speed = 10f;
 
     public LayerMask hitLayers;
     
 
     void Update () 
         {
         Vector3 mouse = Input.mousePosition;
         Ray castPoint = Camera.main.ScreenPointToRay (mouse);
         RaycastHit hit;
 
         if (Input.GetMouseButton(0)) 
         {
             
             if (Physics.Raycast (castPoint, out hit, Mathf.Infinity, hitLayers)) 
             {
                 Vector3 moveTo = new Vector3 (hit.point.x, hit.point.y + 3, hit.point.z);
                 moveThis.transform.position = Vector3.MoveTowards (moveThis.transform.position, moveTo, speed * Time.deltaTime);
 
             }
         }
 
         }
     }
 
Comment
Add comment · Show 3
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 nt314p · Aug 01, 2017 at 01:58 PM 0
Share

So is your game 2D in a sense that you're looking at it from the side? Could you post some pics so we can get a better sense of what you are trying to accomplish?

avatar image Dharke nt314p · Aug 01, 2017 at 02:09 PM 0
Share

lol theres always something i forget to mention.. sorry ... its 3D.. pics wont help much.. all i have at the moment is a cube on a plane.. with a camera looking down at the plane (90 degree rotation).. ie top down orthographic view ... im trying to move the cube (player object) to where the raycast hits the plane from the mouse position

avatar image nt314p Dharke · Aug 03, 2017 at 12:56 AM 0
Share

I believe you can just get the x and y cords of the mouse and move the cube there...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Aug 01, 2017 at 02:22 PM

In calculating the move position:

 Vector3 moveTo = new Vector3 (hit.point.x, hit.point.y + 3, hit.point.z);

you are adding 3 to the y value of hit which is causing your object to elevate from the ground considering your object has size less than 6 units considering the pivot of your object is at its center.

You can use renderer.bounds.extents.y to get the height value to add to your object's moveTo position. Something like:

 Vector3 moveTo = new Vector3 (hit.point.x, hit.point.y + moveThis.GetComponent<Renderer>().bounds.extents.y, hit.point.z);


Comment
Add comment · 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

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

94 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

Related Questions

How to stop Input.GetKey from cancelling mouse raycasting? 0 Answers

Weird mouse look glitch in top down shooter. 0 Answers

Alternative to ScreenPointToRay needed 1 Answer

Directional Shot (!) 1 Answer

Find TextureCoordinates from plane with out raycast only using mouse position on plan 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