- Home /
separate the X and Y axis control systems and use different style of control
I am trying to create a 2D game. However, I am facing some obstacles regarding the control of the game. I want to separate the X and Y axis control systems and use different style of control. I hope to use tapping control to move the character along the Y axis. While I would like to use tilting with accelerometer to control the Left Right movement along the X axis. I am now using two different scripts for the two systems, yet my character is stuck at the left wall without moving up nor down. I have attached my code below. Thank you for your kind help and concern.
Tapping
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class jumping_cat : MonoBehaviour { public float velocity = 1; private Rigidbody2D rb;
// Start is called before the first frame update
void Start(){
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update(){
if(Input.GetMouseButtonDown(0)) { rb.velocity= Vector2.up*velocity; } } }
LeftRight Tilting
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class leftright_tilting : MonoBehaviour {
Rigidbody2D rb;
float dirX;
float moveSpeed = 20f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
dirX = Input.acceleration.x * moveSpeed;
transform.position = new Vector2 (Mathf.Clamp (transform.position.x, -7.5f, 7.5f), transform.position.y);
}
void FixedUpdate()
{
rb.velocity = new Vector2 (dirX, 0f);
}
} ,I am trying to create a 2D game. However, I am facing some obstacles regarding the control of the game. I want to separate the X and Y axis control systems and use different style of control. I hope to use tapping control to move the character along the Y axis. While I would like to use tilting with accelerometer to control the Left Right movement along the X axis. I am now using two different scripts for the two systems, yet my character is stuck at the left wall without moving up nor down. I have attached my code below. Thank you for your kind help and concern.
The tapping
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class jumping_cat : MonoBehaviour { public float velocity = 1; private Rigidbody2D rb;
// Start is called before the first frame update
void Start(){
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update(){
if(Input.GetMouseButtonDown(0)) { rb.velocity= Vector2.up*velocity; } } }
the left right tilting
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class leftright_tilting : MonoBehaviour {
Rigidbody2D rb;
float dirX;
float moveSpeed = 20f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
dirX = Input.acceleration.x * moveSpeed;
transform.position = new Vector2 (Mathf.Clamp (transform.position.x, -7.5f, 7.5f), transform.position.y);
}
void FixedUpdate()
{
rb.velocity = new Vector2 (dirX, 0f);
}
}
Your answer
Follow this Question
Related Questions
inertial navigation system plugin 0 Answers
How can I get the movement when a mobile device moves parallel to the ground? 2 Answers
Gyroscope to move object left and right 1 Answer
Why is the Gyroscope inverted? 0 Answers
How to use an IMU with Unity? 1 Answer