Hello guys,
So I’m making a mobile game in unity for about 2 months now. When I tested it in the unity remote control, everything seemed perfect. But when I built my game with Xcode and installed it on my phone, I noticed a lot of problems that were not there when I tested in the unity remote control. So the problem is that my player does some jittery movement for no reasons(here is a video of it:Imgur: The magic of the Internet). So as you can see in the video when I try to tilt my player to control him, he moves very slowly, he just flies in the air and then there is some kind of lag with the camera. I tried fixing this all day but I could not solve it. Is it a problem with my camera? Maybe its because I added an empty game object and made the camera follow it so that my camera can follow all of my different characters in my game. Is it my player movement script or my level generator script? Can someone pls help me solve this issue. Here are my camera script, player movement script and my level generator script:
Level generator script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class LevelGenerator : MonoBehaviour
{
//list obstacles that can generate
public GameObject[] SpawnablePrefab;
//list of possible position to spawn obstacle
public Vector3[] SpawnablePos;
public int NumberOfObstacles;
public float[] SpawnableProbability;
public int NumberobstacleForFistLevelGenerated = 20;
public int obstacleIncrement = 3;
public void Start()
{
if (DataTracker.instance.Level > 5)
{
NumberOfObstacles = NumberobstacleForFistLevelGenerated + ((DataTracker.instance.Level - 5) * obstacleIncrement);
}
//Calculate positions of possible spawnable objects and store them in the spawnablePos
SpawnablePos = new Vector3[NumberOfObstacles];
for (int i = 0; i < SpawnablePos.Length; i++)
{
SpawnablePos[i] = new Vector3(0, 0, i * 8);
}
GenerateLevel();
}
public void GenerateLevel()
{
//create spawnableobject at each position on spawnable pos
for (int i = 0; i < SpawnablePos.Length; i++)
{
// if its the first 3 spawnablePos, then make appear an empty Spawnable
if (i < 3)
{
Instantiate(SpawnablePrefab[0], SpawnablePos[i], Quaternion.identity);
}
else
{
SpawnRandom(i);
}
}
}
private void SpawnRandom(int Pos)
{
float Probability = Random.Range(0f, 1f);
for (int i = 0; i < SpawnableProbability.Length; i++)
{
if (Probability < SpawnableProbability[i])
{
Instantiate(SpawnablePrefab[i], SpawnablePos[Pos], Quaternion.identity);
return;
}
}
}
}
yer movement script:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody Rb;
public float AccelerationSpeed = 35f;
private float maxSpeed;
public GameObject menuContainer;
[SerializeField] ParticleSystem collectParticle = null;
public float SpeedIncrement = 3f;
public GameObject StartText;
public bool Started = false;
private Vector3 beforePositionPlayer;
public GameObject Player;
int coinsCount;
private void Awake()
{
//Gets all the collected coins once the game starts.
coinsCount = PlayerPrefs.GetInt("saveCoins");
//addCoins.text = "CONSUMED: " + coinsCount;
}
void Start()
{
Rb = GetComponent<Rigidbody>();
Application.targetFrameRate = 60;
StartText.gameObject.SetActive(true);
if (DataTracker.instance.Level > 5)
{
maxSpeed += SpeedIncrement * (DataTracker.instance.Level - 5);
}
else
{
maxSpeed = 35f;
}
}
// Update is called once per frame
void Main()
{
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
Started = true;
StartText.gameObject.SetActive(false);
}
if (Started == true)
{
Vector3 acc = Input.acceleration;
Rb.AddForce(acc.x * AccelerationSpeed, 0, -acc.z * AccelerationSpeed);
Rb.velocity = Vector3.ClampMagnitude(Rb.velocity, maxSpeed);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("killzone"))
{
menuContainer.SetActive(true);
Collect();
}
}
public void Collect()
{
collectParticle.Play();
}
void Update()
{
Vector3 playerPos = transform.position;
playerPos.y= Mathf.Clamp(gameObject.transform.position.y, -15, 0);
beforePositionPlayer = Player.transform.position;
}
// if user no watch ad.
public void KillPlayer()
{
Destroy(Player);
}
//If user watch add.
public void RespawnPlayer()
{
Player.transform.position = new Vector3(beforePositionPlayer.x+3, beforePositionPlayer.y, beforePositionPlayer.z + 22);
Player.transform.position = beforePositionPlayer;
}
}
Camera script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
//the player's current position
public Transform player;
// distance between player
public Vector3 offset;
private void Start()
{
player = GameObject.FindGameObjectWithTag("camerafollow").transform;
}
private void Update()
{
//the camera will move but now it will be in front of the player not behind following it
transform.position = player.position + offset;
}