I am working on a rail shooter. I have the camera working as wanted, and have an on screen recticle moving as wanted too, except that I want to have it smoothly lerp back to the center of the screen when the joystick is released. I know what I have here will not work as the movement is based on the joystick position, can anyone tell me how to do this?
using UnityEngine;
using System.Collections;
public class Joystick_Target : MonoBehaviour {
public string Stick;
public GameObject Target;
public float Speed = 1.0F;
void Update ()
{
const float smooth = 5.0F;
const float tiltAngle = -45.0F;
const float targetAngle = 45.0F;
// See InputManager (Edit > Project > Input) for mapping from <string> to <axis>
// Set up x/y coords for joystick and recticle
float x = Input.GetAxis (Stick + " Stick Horizontal") * tiltAngle;
float y = Input.GetAxis (Stick + " Stick Vertical") * tiltAngle;
float xTarget = Input.GetAxis (Stick + " Stick Horizontal") * targetAngle;
// Move Camera
Quaternion target = Quaternion.Euler (y, x, 0);
transform.rotation = Quaternion.Slerp (transform.rotation, target, Time.deltaTime * smooth);
// Move Recticle
var move = new Vector3 (xTarget, y, 0);
var home = new Vector3 (0, 0, 0);
Target.transform.position += move * Speed * Time.deltaTime * smooth;
// Check for dead stick
if (x == 0 && y == 0)
Target.transform.position -= home * Speed * Time.deltaTime * smooth;
}
}
What I ended up doing was creating object at the point I wanted the recticle to return to (because vector3.zero is off screen), and doing this:
if (x <= 0.25 && y <= 0.25)
myguiText.text = (tX + ", " + tY + ", " + tZ + "DZ");
Target.transform.position = (Vector3.MoveTowards (Target.transform.position, Home.transform.position, Speed));
if (Vector3.Distance (Target.transform.position, Home.transform.position) < 0.001f)
Target.transform.position = Home.transform.position;
Which works pretty much perfectly. Thanks for your help anyway, now I gotta force it to stay in the screen bounds.
system
2
if input == null
camera.transform.position = vector3.zero;