- Home /
Question by
tom-moon · Jun 08, 2017 at 03:34 PM ·
2dvector3vector2movetowardsvector3.movetowards
conserve momentum of vector3.moveTowards in 2D game
I have a character who is hanging in mid air with a gravitational force constantly pulling them downwards. however, when a mouse click is detected, the character accelerates towards the click. I have written the following code.
public class controller : MonoBehaviour {
private Vector3 target;
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
float startTime;
float elapsedTime;
bool gravOn = true;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void FixedUpdate () {
if (gravOn == true) {
elapsedTime = Time.time - startTime;
velocity += gravity * elapsedTime;
transform.position += velocity * elapsedTime;
}
if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) || (Input.GetMouseButton (0))) {
gravOn = false;
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, target, 0.5f);
startTime = Time.time;
} else {
gravOn = true;
}
}
}
my problem is, when I release the mouse the character instantly stops accelerating towards the mouse click and instantly starts falling again. I would like the momentum of the acceleration caused by vector3.movetowards to be conserved, so the character could be "thrown" by the moveTowards function. What am i missing here?
Comment
Your answer
Follow this Question
Related Questions
How to use 2D pathfinding with vector 2 0 Answers
Normalizing a vector 1 Answer
Overload Vector3's operator + 2 Answers
Smoothly moving a 2D gameobject along a vector path 1 Answer