- Home /
No coding experience, tried to mash two codes together, but they aren't playing nicely
So the idea is to get the cube i'm currently using to move 1.25 right and -1.25 left which I fixed in its code, and the other one uses touch in order to move the character, but after trying to export an .apk it doesn't seem to work. Would really appreciate some help! Attached is the whole code I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
//variables
public GameObject character;
private float speed = 5.0f;
private Vector3 pos;
private Transform tr;
private Rigidbody2D characterBody;
private float ScreenWidth;
// Use this for initialization
void Start()
{
pos = transform.position;
tr = transform;
ScreenWidth = Screen.width;
characterBody = character.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > ScreenWidth / 2 && tr.position == pos)
{
//move right
pos += new Vector3(1.25f, 0, 0);
}
if (Input.GetTouch(i).position.x < ScreenWidth / 2 && tr.position == pos)
{
//move left
pos += new Vector3(-1.25f, 0, 0);
}
++i;
}
}
}
Problem is as soon as tr.position is not equal to pos your code will stop working. Basically your code will start and check is current position is same as starting position,once you tap that would be true for a split seconds until code hit next line : pos+= new Vector3(1.25f,0,0),right that moment tr.position is not equal to pos so your pos+= new Vector3(1.25f,0,0) will not work,that code will not be executed.
Answer by tormentoarmagedoom · Sep 15, 2018 at 12:36 PM
Good day.
You say when yo uexport does not work, but, it works in the editor?
I did not read all the code, dont know what you want to get, and never did a project for mobile, so Input.touch family functions are a mistery for me. But, if you read the manual will see some incongruences
[....]
void Update()
{
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
[....]
If you read the Input.touchCount manual the only it says is:
Number of touches. Guaranteed not to change throughout the frame. (Read Only)
So, if Update is called only once per frame, the value of touchCount will be always the same during that frame.
It can be this? Did you debuged the code to see how the number increases?
Bye!