- Home /
Delay when drag object from mouse cursor
Hi guys I've found this scrip on web, this is a attacked to my player. it's for simple drag and shot game. It's work fine but i would like that the gameobject follow the mouse cursor always in the center of this. Now there's a delay when I drag it away from the center of gameobject ( Increases the distance between him and the cursor).
Can someone help me?
public Vector3 gameObjectSreenPoint;
public Vector3 mousePreviousLocation;
public Vector3 mouseCurLocation;
public void OnMouseDown() { //This grabs the position of the object in the world and turns it into the position on the screen //gameObjectSreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); gameObjectSreenPoint = gameObject.transform.position; //Sets the mouse pointers vector3 mousePreviousLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.z, Input.mousePosition.y);
}
public Vector3 force;
public Vector3 objectCurrentPosition;
public Vector3 objectTargetPosition;
public float topSpeed = 10;
void OnMouseDrag()
{
mouseCurLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.z, Input.mousePosition.y);
force = mouseCurLocation - mousePreviousLocation;//Changes the force to be applied
mousePreviousLocation = mouseCurLocation;
}
public void OnMouseUp()
{
//Makes sure there isn't a ludicrous speed
if (this.gameObject.GetComponent<Rigidbody>().velocity.magnitude > topSpeed)
force = this.gameObject.GetComponent<Rigidbody>().velocity.normalized * topSpeed;
}
public void FixedUpdate()
{
this.gameObject.GetComponent<Rigidbody>().velocity = force;
}
}
Your answer
Follow this Question
Related Questions
Rigidbody Not Move 0 Answers
i cant see my texture when i got to select option ? 0 Answers
I drag a rotated object to a parent - and it has totally different rotation. 1 Answer
Following instruction for drag and drop 3d object unity but not working 1 Answer
Jigsaw Puzzle Drag & Drop issues. 0 Answers