i have a script that moves player with/as mouse , what I need is to apply gravity and other drag etc physics motions to it,
when this script is attached to gameobject gravity doesn’t work/apply since mouse take control over the rest I guess when I place the rigidbody 2d gravity nothing happens
so how do I apply gravity and or drag and other similar related physics?
this is 2d and javascript
#pragma strict
var myCamera : Camera;
function Start () {
}
function Update () { //rigidbody2D.AddForce (Vector3 (vec.x * force_factor, vec.y * force_factor, 0));
var vec : Vector3 = myCamera.ScreenToWorldPoint(Input.mousePosition);
var side : float = 8.0;
var top : float = 9.0;
vec.x = Mathf.Clamp(vec.x, -side, side);
vec.y = Mathf.Clamp(vec.y, -top, top);
vec.z = 0.0;
transform.position = vec;
}
You will want to track the mouse’s delta movement rather than the position. If you’re constantly setting the object to the mouse’s position it won’t matter what kind of forces you place on it - it will always be at the mouse’s position.
Input.GetAxis ("Mouse X") // Mouse's x delta movement
Input.GetAxis ("Mouse Y") // Mouse's y delta movement
I took a bit of a guess with my answer. I’m assuming that your object won’t attempt to move back to the mouse’s position. If it does you will need to use forces to move it to the mouse’s position rather than setting it directly (in which case you can ignore the delta values).
void FixedUpdate()
{
//float move = Input.GetAxis (“Horizontal”);old no good for mouse line but good for grounded
//Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
//rigidbody2D.AddForce (movement);
Input.GetAxis (“Mouse X”) ;// Mouse’s x delta movement
Input.GetAxis (“Mouse Y”) ;// Mouse’s y delta movement
rigidbody2D.AddForce (Vector3 (Mouse X * force_factor, Mouse Y * force_factor, 0));