# Use force instead of transform.position

Hello I have a mouse follow script which is use static velocity. But i want to transform it to force physics How can i do it.

My Script is ;

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

{
#region DEGİSKENLER

``````private Vector3 Position;
RaycastHit hit;
Ray ray;
public float speed = 10f;
private int sayac = 0;
private bool durum = false;
private int durum2 = 0;
int position = 0;
private bool flag = false;
private Vector3 endPoint;
public float duration = 50.0f;
private float yAxis;
private Rigidbody rb;
#endregion DEGİSKENLER

#region OLAYLAR
void Start()
{
yAxis = gameObject.transform.position.y;
Rigidbody rb = GetComponent<Rigidbody>();
}

void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit))
{
flag = true;

endPoint = hit.point;

endPoint.y = yAxis;
}

if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
if (sayac % 10 == 0)
{
position = Random.Range(10, 20);
durum2 = Random.Range(0, 1);
if (durum2 == 0)
{
position = position * -1;
}

if (durum)
{
float xx = position;
endPoint.x -= xx;
durum = false;
}
else
{

float xx2 = (float)position;
endPoint.z -= xx2;
durum = true;
}

;
//gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));

for (int wait = 0; wait < 5000000; wait++)
{

}
}
else
{
//gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));
}
sayac++;
}

else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
flag = false;
Debug.Log("I am here" + sayac.ToString());
}
}
#endregion OLAYLAR
``````

}

I would take the difference between the mouse hit point position, and the following-object’s position. (uncomplied, example only code)

``````Vector3 diff = hit.point - gameObject.transform.position;
``````

If we don’t want the distance to effect the force, normalize our diff vector (set length = 1)

``````diff.Normalize();
``````

Now apply force in that direction:

``````RigidBody rb = gameObject.GetComponent<RigidBody>();
if(rb!=null)
rb.AddForce(diff*forceMagnitude); //if we did not normalize diff earlier, this force's final magnitude would scale with the distance (might be desirable)
``````