-1
\$\begingroup\$

I wanted to control my character with dragging controls (horizontally only) I currently have a code that controls the character with touching the left and right screen. I wanted the same controls as the game "Everwing" . I cannot find anything on youtube so I'm having a hard time with this. Please can anyone help me I'm still just a begginer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class meteorController : MonoBehaviour {
public float meteorSpeed;
public float maxPos = 2.2f;

Vector3 position;
public uiManager ui;
public AudioManager am;

bool currentPlatformAndroid = false;

Rigidbody2D rb;

void Awake() {

    rb = GetComponent<Rigidbody2D>();

#if UNITY_ANDROID
        currentPlatformAndroid = true;
#else
    currentPlatformAndroid = false;
#endif

    am.level1Music.Play();
}

// Use this for initialization
void Start() {
    position = transform.position;

    if (currentPlatformAndroid == true) {
        Debug.Log("Android");
    }
    else {
        Debug.Log("Windows");
    }



}

// Update is called once per frame
void Update() {

    if (currentPlatformAndroid == true)
    {
        //android specific
        TouchMove();
        //AccelerometerMove();

    }
    else
    {
        position.x += Input.GetAxis("Horizontal") * meteorSpeed * Time.deltaTime;

        position.x = Mathf.Clamp(position.x, -2.2f, 2.2f);

        transform.position = position;
    }
    position = transform.position;
    position.x = Mathf.Clamp(position.x, -2.2f, 2.2f);
    transform.position = position;
}

void OnCollisionEnter2D(Collision2D col) {
    if (col.gameObject.tag == "asteroid") {
        Destroy(gameObject);
        ui.gameOverActivated();
        am.level1Music.Stop();
    }
}

void AccelerometerMove() {

    float x = Input.acceleration.x;


    if(x < -0.1f){
        MoveLeft();
    } else if (x > 0.1f) {
        MoveRight();
    }
    else {
        SetVelocityZero();
    }
}

void TouchMove(){
    if(Input.touchCount > 0){

        Touch touch = Input.GetTouch(0);

        float middle = Screen.width / 2;

        if(Input.touchCount >= 2){
            SetVelocityZero();
        }

        if(touch.position.x < middle && touch.phase == TouchPhase.Began) {
            MoveLeft();
        }
        else if (touch.position.x > middle && touch.phase == TouchPhase.Began) {
            MoveRight();
        }

    }
    else {
        SetVelocityZero();
    }
}

public void MoveLeft(){
    rb.velocity = new Vector2(-meteorSpeed, 0);
}
public void MoveRight(){

    rb.velocity = new Vector2(meteorSpeed, 0);
}
public void SetVelocityZero(){
    rb.velocity = Vector2.zero;
}

}

Here is how my game looks like and I wanted the character (The meteor) to move left and right.

enter image description here

\$\endgroup\$
-1
\$\begingroup\$

I used a script that I got from "rudisky" in this post. It worked well for me

Here is the script:

using UnityEngine; 
using System.Collections;

public class movimiento : MonoBehaviour {

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () 
 {

 //Gets the world position of the mouse on the screen        
 Vector2 mousePosition = Camera.main.ScreenToWorldPoint( Input.mousePosition );

 //Checks whether the mouse is over the sprite
 bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains( mousePosition );

 //If it's over the sprite
 if (overSprite)
 {
     //If we've pressed down on the mouse (or touched on the iphone)
     if (Input.GetButton("Fire1"))
     {
         //Set the position to the mouse position
         this.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
             Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
             0.0f);
      }
    }
  }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .