[Android] Character movement script help
Finding exactly what I need has proven difficult. I have a working script, but it doesn't function exactly the way I want it to.
Desired Functionality:
Character moves to the user's finger and "floats" past a bit, as if floating in space with little to no gravity. After moving past the user's finger, the character slowly moves back to the finger location. The character follows the user's finger at all times, as long as the finger is on the screen. Following should be precise.
Production Functionality:
Character moves towards user's finger, but does not stop, nor turn back towards the finger. Character continues moving in general direction of the finger and does not stop until it slams into the side of the screen. Once it has hit the side of the screen, it takes a moment to get the character to follow the finger again. Following is not precise, and seems to be off by some degree. The character will fly towards the stationary finger, but it, more often than not, will be off on the y axis by a few degrees.
// BEGIN MOVEMENT VARIABLE SECTION
public float speed = .06f;
Vector2 currentPos;
float xmin;
float xmax;
float ymin;
float ymax;
private Vector3 finger;
private Transform myTrans, camTrans;
Rigidbody2D rb;
float force;
// END MOVEMENT VARIABLE SECTION
void Update ()
{
print (Input.GetTouch (0).phase + " touchPhase");
//TOUCH INPUT CONTROLLER
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
// begin animation and movement scripting
Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
finger = Camera.main.ScreenToWorldPoint (tempTouch);
//myTrans.LookAt (finger);
myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
rb.AddForce (transform.forward * force);
//print (myTrans.transform.position + " mytrans");
//print (finger + " finger");
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
// begin animation and movement scripting
Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
finger = Camera.main.ScreenToWorldPoint (tempTouch);
//myTrans.LookAt (finger);
myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
rb.AddForce (transform.forward * force);
//print (myTrans.transform.position + " mytrans");
//print (finger + " finger");
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
// begin animation and movement scripting
Vector3 tempTouch = new Vector3 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y, camTrans.position.y - myTrans.position.y);
finger = Camera.main.ScreenToWorldPoint (tempTouch);
//myTrans.LookAt (finger);
myTrans.Translate (finger.x * speed * Time.deltaTime, finger.y * speed * Time.deltaTime, 0);
rb.AddForce (transform.forward * force);
//print (myTrans.transform.position + " mytrans");
//print (finger + " finger");
}
//TOUCH INPUT CONTROLLER
//CLAMP TO SCREEN CONTROLLER
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance));
xmin = leftmost.x + .5f;
xmax = rightmost.x - .5f;
Vector3 topmost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
Vector3 bottommost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, distance));
ymin = topmost.y + .5f;
ymax = bottommost.y - .5f;
// restrict the player to the gamespace
float newX = Mathf.Clamp (transform.position.x, xmin + .25f, xmax - .25f);
float newY = Mathf.Clamp (transform.position.y, ymin + .5f, ymax - .5f);
transform.position = new Vector3 (newX, newY, transform.position.z);
//CLAMP TO SCREEN CONTROLLER
}
The clamp is working fine, but I figured I'd include it in case you needed it.
I've pieced this code together over hours and hours of video and Unity answers and Stack Exchange scouring.
Any input would be greatly appreciated.
Answer by lol2dubs · Dec 30, 2016 at 06:40 PM
I've figured this out for anyone who is interested. It's a much more elegant solution, as well.
//STARSPEED CONTROLLER
if (starCollide == true) {
speed = 12.0f;
} else {
speed = 5.5f;
}
//STARSPEED CONTROLLER
//TOUCH INPUT CONTROLLER
if (transform.position != realPos) {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, realPos, step);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began || Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (0).phase == TouchPhase.Stationary) {
Vector3 fingerPos = Input.GetTouch (0).position;
tempPos = fingerPos;
tempPos.z = 0;
realPos = Camera.main.ScreenToWorldPoint (tempPos);
realPos.z = 0;
rb.AddForce (transform.forward * force);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
realPos = transform.position;
}
//TOUCH INPUT CONTROLLER