I’m having an issue with a player (paddle) being able to rotate on PC as intended but when I make a build for android it doesn’t allow the player to do so. I haven’t seen any errors in the script, and the Debug logs on both PC and Android Debugger show no errors. I’m not sure what could be the cause.
The only area that controls and affects the rotation is in the MovePaddle() method, I believe x.X.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Paddle : MonoBehaviour
{
public float paddleSpeed;
Rigidbody rb;
public static Paddle Instance { get; private set; }
public static int coinsInGame;
private float screenWidth;
float smooth = 5.0f;
float tiltAngle = 60.0f;
public GameObject hazardHitFx;
// Start is called before the first frame update
void Start()
{
Instance = this;
rb = GetComponent<Rigidbody>();
screenWidth = Screen.width;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
// Update is called once per frame
void Update()
{
if (SystemInfo.deviceType == DeviceType.Handheld)
{
int i = 0;
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > screenWidth / 2)
{
MovePaddle(100f);
}
if (Input.GetTouch(i).position.x < screenWidth / 2)
{
MovePaddle(-100f);
}
++i;
}
}
else
{
if (!Input.anyKey)
{
MovePaddle(0f);
paddleSpeed = 0f;
}
if (Input.GetKey("a"))
{
paddleSpeed = 50f;
MovePaddle(-100f);
}
if (Input.GetKey("d"))
{
paddleSpeed = 50f;
MovePaddle(100f);
}
}
}
void FixedUpdate()
{
#if UNITY_EDITOR
MovePaddle(Input.GetAxis("Horizontal"));
#endif
}
public void MovePaddle(float horizontalInput)
{
//move left and right
rb.AddForce(new Vector3(horizontalInput * paddleSpeed * Time.deltaTime, 0, 0));
//check the debug on the android device..
//Debug.Log(horizontalInput);
float tiltAroundX = Input.GetAxis("Horizontal") * tiltAngle / 2.5f;
// Rotate the player by converting the angles into a quaternion.
Quaternion target = Quaternion.Euler(tiltAroundX, 90, 0);
// Dampen towards the target rotation
rb.transform.rotation = Quaternion.Slerp(rb.transform.rotation, target, Time.deltaTime * smooth);
}
//collide with coins or other drops..
void OnCollisionEnter(Collision collision)
{
//Debug.Log("Coins");
if (collision.gameObject.tag == "Coins")
{
GameManager.Instance.Coins += 100;
if (GameManager.Instance.isCoinBall == true)
{
GameManager.Instance.Coins += 100;
}
Destroy(collision.gameObject);
}
if (collision.gameObject.tag == "Hazard")
{
if(GameManager.Instance.isEdgePaddle == false)
{
StartCoroutine(Freeze());
}
Destroy(collision.gameObject);
}
}
IEnumerator Freeze()
{
Instantiate(hazardHitFx, transform.position, Quaternion.identity);
rb.constraints = RigidbodyConstraints.FreezePosition | RigidbodyConstraints.FreezeRotation;
paddleSpeed = 0;
GameManager.Instance.FlashFreeze();
yield return new WaitForSeconds(1.5f);
//could set a speed off the start speed instead, then hardcode it so its not effected by whats set in editor
paddleSpeed = 50;
rb.constraints = RigidbodyConstraints.None;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
}
}