Rotate Object Axis On Swipe Cam Rotation - Android
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!
![alt text][1] [1]: /storage/temp/88679-testball.jpg
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;
}
}
}
Answer by Molmez · Feb 23, 2017 at 10:19 PM
I tried to use this but ball randomly goes nuts in spinning.
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);
What I am thinking is addforce is world space. even if i addrelativeforce the ball itself rotates when moving so that doesnt work.
Either I need to somehow rotate the level on swipe instead of the camera or I need to use transform forward and rotate the ball inside an animation. Any opinions on this before I end up in therapy?
thanks.
Your answer
Follow this Question
Related Questions
Move an object by rotation 0 Answers
Having two objects Y axis parallel rotating around local Z axis. 0 Answers
Player Rotation Around Mouse 0 Answers
Creating own "x" and "z" axis to move player along? 0 Answers