How do I get my object to move towards the mouse without using raycasts or transform?
Hello! I wrote out some code for this problem, but I'm new to programming in unity and feel like I made a simple error but can't seem to locate it.
I'd prefer to not use raycast's or transform because I'd like the object to maintain momentum from the simple physics system attached to it. I think the error may be attached to the "GetComponent().position" but am not sure how to reference the attached objects position (Also the error in unity just references the whole block starting at Input.GetMouseButton(0)) Thank you in advance!
Answer by tormentoarmagedoom · Aug 03, 2018 at 09:44 PM
Good day.
I dont give you the answer, i show you the doors you must explore, and some tips:
First, Declare the variables Camera and the 3 floats at the Start() method, as only need to be done once, no need to do every frame you are entering this part of the code.. is not a good practice for memory issues.
Second, expolore this: https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
Third, You need transform for moving something. All objects in the scene ARE a transform, it means a position a rotation and a scale, so you need to use it. You have so many ways to do it.
transform.Translate()
transform.position += new Vector3 (0,1,0);
And using phisycs, by adding forces to the rigidbody.
Go learn! Bye!
Thank You! That certainly was a massive oversight, from now on I'll try to include all declarations in start, I was looking at the documentation for ScreenToWorldPoint before, but am having trouble figuring out how to find the location of my object relative to the camera/screen. I think I may have used the ter$$anonymous$$ology wrong in respect to excluding transform, I'm using to move my object, and I just wanted to exclude any solution options that required me to move my object at a uniform pace, or to a set location. Since the object won't be moving exactly where you click as it is influenced by the momentum previously established (I also see now I must change the Horizontal/Vertical to something like HorVel = HorVel + (5 * xfac);
Your answer
![](https://koobas.hobune.stream/wayback/20220612172141im_/https://answers.unity.com/themes/thub/images/avi.jpg)