Hey peeps,
I am trying to make a game similar to Kula world but not exactly as im not quite sure how to custimize the gravity to the level as of yet.
For now I am just sticking to one surface of movement. Using mobile tilt to move forward and backward based on y tilt.
I have incorporated a swipe function to rotate the camera. I want to use this to then move froward along a new path that the user has rotated the camera to view.
I have tried various solutions based on similar scenarios but none match what im doing and am slightly confused.
Basically when the user rotates the camera left or right I want the forward and back vectors to adjust to the view of the camera.
So in this simple screenshot you would move forward to the cross section, swipe to rotate camera then move forward along new path. However right now when you swipe camera it changes nothing you now appear to move left and right. I tried rotating the balls Y axis with camera Y but this causes some crazy behavoir and doesnt effect direction.
Thank You!
Camera Code:
public class CameraMovement : MonoBehaviour {
public Transform lookAt;
private Vector3 offset;
private float distance = 5.0f;
private float yOffset = 3.5f;
private Vector2 touchPosition;
private float swipeResistance = 200.0f;
private void Start(){
offset = new Vector3 (0, yOffset, -1f * distance);
}
private void Update(){
if (Input.GetMouseButtonDown (0) || Input.GetMouseButtonDown (1))
{
touchPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp (0) || Input.GetMouseButtonUp (1))
{
float swipeforce = touchPosition.x - Input.mousePosition.x;
if (Mathf.Abs (swipeforce) > swipeResistance)
{
if (swipeforce < 0)
{
slideCamera (true);
}
else
{
slideCamera (false);
}
}
}
transform.position = lookAt.position + offset;
transform.LookAt (lookAt);
}
public void slideCamera(bool left){
if (left)
offset = Quaternion.Euler (0, 90, 0) * offset;
else {
offset = Quaternion.Euler (0, -90, 0) * offset;
}
}
}
Ball Movement Code:
// Update is called once per frame
void Update () {
y = Input.acceleration.y;
if (y > 0.1f)
{
controller.AddForce (Vector3.back * Speed * Time.deltaTime);
}
else if (y < -0.1f)
{
controller.AddForce (Vector3.forward * Speed * Time.deltaTime);
}
else
{
controller.velocity = Vector3.zero;
}
}
}