2

Scene: scene image

Hi I'm trying to control the hole along the X and Z axis with using the mouse but don't want the hole moving to the mouse position

Plane m_Plane;

private Vector3 newmousepos;


void Start()

{

    m_Plane = new Plane(Vector3.up, transform.position);

}

void Update()

{
    if (Input.GetMouseButton(0))

    {

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          float enter = 0.0f;

          if (m_Plane.Raycast(ray, out enter))

           {
               //Get the point that is clicked
               Vector3 hitPoint = ray.GetPoint(enter);

              transform.position = 
              Vector3.Lerp(transform.position,hitPoint,Time.deltaTime*5f);

           }  

    }
RV Binary
  • 23
  • 4
  • 3
    you say you dont want it moving to the mouse, what exactly do you need it to do then? – BugFinder Jul 10 '19 at 13:57
  • I'm guessing you want it to move in the same pattern as the mouse? So if mouse moves x+5 the hole should move x+5? – Fredrik Schön Jul 10 '19 at 14:37
  • 1
    You don't need to raycast. Instead use Input.GetAxis – Leo Bartkus Jul 10 '19 at 14:38
  • Looking at the code, I think my guess is incorrect..? I think OP is having issues with the lerp? It probably instantly teleports to his position since the lerp is done incorrectly. – Fredrik Schön Jul 10 '19 at 14:39
  • my point is that i just want to control the hole with using the mouse when the pointer is in a random position in the scene but i don't know how to avoid hole move to mouse position – RV Binary Jul 11 '19 at 06:19

1 Answers1

1

Edit: New answer based on new informaton

I will keep the old answer since the code is not functional without it, if "move towards mouse position" would be the desired behaviour, as the title suggests.

Problem

You're trying to get the object to do the same movements as the mouse is doing, but the code you've written takes in the position of the mouse, not the movement of the mouse, and moves towards it.

Solution

You must check the axis of the mouse, and apply that movement direction to the object, something like:

float speed = 5f;
void Update()
{
    if (Input.GetMouseButton(0))
    {
        var moveDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
        transform.position += moveDirection * speed * Time.deltaTime;
    }
}


Old answer

Problem

I'm guessing your Lerp isn't doing what you want it to do, since the code will only execute once, and is setting your position to Time.deltaTime*5, which should roughly be 0.9, which means 90% of the distance from transform.position to hitPoint. For more information about Lerping, please read the documentation:

Vector3 Lerp(Vector3 a, Vector3 b, float t);

  • When t = 0: returns a.
  • When t = 1 returns b.
  • When t = 0.5 returns the point midway between a and b.

Solution

I think a simple Vector3.MoveTowards will work for you:

Vector3 hitPoint;
float speed = 5;

void Update()
{
    if (Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        float enter = 0.0f;

        if (m_Plane.Raycast(ray, out enter))
        {
            hitPoint = ray.GetPoint(enter);
            transform.position = Vector3.MoveTowards(transform.position, hitPoint, speed * Time.deltaTime);
        }
    }
}

Community
  • 1
  • 1
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
  • thanks for your help but still object moves to mouse positions – RV Binary Jul 11 '19 at 06:22
  • I'm not sure exactly what you want, would you like to confirm that what you wish is for the object to do the same movements as the mouse, but relative to its own position, not the mouse? – Fredrik Schön Jul 11 '19 at 09:02
  • I edited my answer to include an answer for that scenario. – Fredrik Schön Jul 11 '19 at 09:14
  • i'm sorry i can't explain it well, assume that you can put your finger in a specific coordinate of screen and control the hole object along the scene in X and Z axis. i found this game that is very similar to what thing i seek https://play.google.com/store/apps/details?id=com.h8games.blocksbuster – RV Binary Jul 11 '19 at 10:05
  • Should I interpret the fact that you don't confirm my question that I am misunderstanding? – Fredrik Schön Jul 11 '19 at 14:09
  • Just saw your "answers"; just put `Mouse Y` on the `z` property of the Vector3, if you want mouse Y Movement to manipulate Z... There's no such thing as `Mouse Z`; how do you move the mouse in Z axis? Lift it? :D – Fredrik Schön Jul 11 '19 at 14:10
  • I edited my answer to move Mouse Y to the Z property – Fredrik Schön Jul 11 '19 at 14:11