0
\$\begingroup\$

I have script moving object to mouse postion using raycast. But what I need now is use the mouse movement to move my object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HoverPanel : MonoBehaviour
{

public GameObject ground;
private Rigidbody rg;
public float moveForce, speed = 2;
public Vector3 Mouse_pos;

void Start()
{
    rg = gameObject.GetComponent<Rigidbody>();
}

void Update()
{

    rg.AddForce(Input.GetAxis("Vertical") * moveForce * ground.transform.forward);

    if (Input.GetAxis("Mouse X") < 0)
    {
        //Code for action on mouse moving left
        print("Mouse moved left z");
    }
    if (Input.GetAxis("Mouse X") > 0)
    {
        //Code for action on mouse moving right
        print("Mouse moved right z");
    }
    if (Input.GetAxis("Mouse Y") < 0)
    {
        //Code for action on mouse moving left
        print("Mouse moved back x ");
    }
    if (Input.GetAxis("Mouse Y") > 0)
    {
        //Code for action on mouse moving right
        print("Mouse moved forward x");
    }


}

void FixedUpdate()
{

  /// No need to use this 
  /*
    RaycastHit hitGround;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hitGround, 500))
    {
        Mouse_pos = new Vector3(hitGround.point.x, transform.position.y, hitGround.point.z);
        transform.localPosition = Vector3.Lerp(transform.localPosition, Mouse_pos, speed * Time.deltaTime);
    }
  */
}
  

}

\$\endgroup\$
2
  • \$\begingroup\$ It looks like you're already detecting the four movement directions. Presumably you tried applying a force, velocity, or position change to your rigidbody inside those if clauses, in place of those print statements? What did you try, and how did the behaviour differ from what you want? \$\endgroup\$ – DMGregory Apr 9 at 15:51
  • \$\begingroup\$ I can't get a smooth movement like that I get from the Lerp with raycast. \$\endgroup\$ – user43474 Apr 9 at 16:02
1
\$\begingroup\$
public class MoveWithMouseDirection : MonoBehaviour
{
    public float speed = 2f;
    private Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();        
    }

// Update is called once per frame
void Update()
{
    //Get the mouse axis input, turn it into a vector3 so we can simpifly movement.
    //normalize the vector3
    Vector3 mouseDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y")).normalized;

    //Use the direction however you want
    //Make sure to multiply it by Time.deltaTime so it stays in sync with frame rate
    transform.Translate(mouseDirection * speed * Time.deltaTime);

    //or 
    //rb.AddForce(mouseDirection * speed * Time.deltaTime);
}
}

Hopefully the comments are self explanatory but let me know if I didnt explain anything well enough.

But the gist of it is, we turn the mouse axis input into a Vector3 and use that to drive movement.

If you want to smooth the movement out you can use Vector3.MoveTowards or Vector3.Lerp (the exact same way you did for the local position) I opted to used MoveTowards

With Smooth Interpolation

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveWithMouseDirection : MonoBehaviour
{
    public float speed = 2f;

    // Update is called once per frame
    void Update()
    {
        //Get the mouse axis input, turn it into a vector3 so we can simpifly movement.
        //normalize the vector3
        Vector3 mouseDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y")).normalized;


        Vector3 lerpedValue = Vector3.MoveTowards(transform.position, transform.position - mouseDirection, speed * Time.deltaTime);
        transform.position = lerpedValue;
    }
}

You should change Vector3 lerpedValue = Vector3.MoveTowards(transform.position, transform.position - mouseDirection, speed * Time.deltaTime); and either add or subtract the mouseDirection depending on wether you want the object to move in the mouse direction or opposite to it.

I also removed the rigidbody as this doesn't seem to be an object that would exist physically in-scene, the name Hover Panel makes me think this is UI so changing the transform is more appropiate.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the script. It's work but I need something give me smooth movement like Lerp. Could I get this? \$\endgroup\$ – user43474 Apr 9 at 16:08
  • \$\begingroup\$ Ok sure, are you using a rigidbody or the transform to move these objects? \$\endgroup\$ – Pheonix2105 Apr 9 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.