Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gyroscope Senior Developer

Discussion in 'Commercial: Job Offering' started by brano42, Nov 30, 2022.

  1. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    Hello, I'm looking for a game developer who can edit my script .... I need to turn the axis on my fighter using Gyroscope ....my problem is that my fighter turns, but when I level my mobile, it returns to the original position ...Can anyone help me? here is script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6.  
    7. public class F22Movoment : MonoBehaviour
    8. {
    9.  
    10.     private Gyroscope phoneGyro;
    11.     private Quaternion correctionQuaternion;
    12.     public static float Myspeed;
    13.     public Rigidbody m_Rigidbody;
    14.     public float speed;
    15.     public GameObject[] plane;
    16.     public GameObject rocket;
    17.     public Transform RocketSpawn;
    18.  
    19.     public Quaternion planesQuaternion;
    20.  
    21.     public float maxSpeed = 1000;
    22.     public bool rotationbool;
    23.     public float horizontal;
    24.     public Quaternion calculatedRotation;
    25.     public float right;
    26.     public float left;
    27.     void Start()
    28.     {
    29.         m_Rigidbody = GetComponent<Rigidbody>();
    30.         phoneGyro = Input.gyro;
    31.         correctionQuaternion = Quaternion.Euler(40f, 0f, 40f);
    32.         phoneGyro.enabled = true;
    33.  
    34.     }
    35.  
    36.    public void ShootRocket()
    37.     {
    38.  
    39.         var bullet = PhotonNetwork.Instantiate(rocket.name, RocketSpawn.position, RocketSpawn.rotation);
    40.             bullet.GetComponent<Rocket>().linkedPlayer = PhotonNetwork.LocalPlayer.ActorNumber;
    41.  
    42.     }
    43.     void Update()
    44.     {
    45.         horizontal = Input.GetAxis("Horizontal") + transform.rotation.z;
    46.         speed += 1;
    47.         Myspeed = m_Rigidbody.velocity.magnitude;
    48.         if (Myspeed > maxSpeed)
    49.         {
    50.             GyroModifyplayer();
    51.          
    52.  
    53.  
    54.  
    55.         }
    56.         m_Rigidbody.AddForce(transform.forward * speed * Time.deltaTime);
    57.  
    58.         if (m_Rigidbody.velocity.magnitude > maxSpeed)
    59.         {
    60.             m_Rigidbody.velocity = m_Rigidbody.velocity.normalized * maxSpeed;
    61.         }
    62.  
    63.         for (int i = 0; i < plane.Length; i++)
    64.         {
    65.  
    66.             planesQuaternion.z = transform.rotation.z;
    67.             planesQuaternion.y = transform.rotation.y;
    68.             plane[i].transform.rotation = planesQuaternion;
    69.  
    70.         }
    71.  
    72.  
    73.     }
    74.  
    75.     void GyroModifyplayer()
    76.     {
    77.         Quaternion gyroQuaternion = GyroToUnity(Input.gyro.attitude);
    78.         calculatedRotation = correctionQuaternion * gyroQuaternion;
    79.  
    80.  
    81.  
    82.  
    83.  
    84.         if (horizontal < 0.1f & horizontal > -0.1)
    85.         {
    86.             rotationbool = true;
    87.  
    88.  
    89.  
    90.         }
    91.         else
    92.             rotationbool = false;
    93.  
    94.         if (rotationbool == true)
    95.         {
    96.            
    97.              gameObject.transform.rotation = new Quaternion(calculatedRotation.x, calculatedRotation.y, calculatedRotation.z, 1);
    98.  
    99.         }
    100.  
    101.         if (horizontal < 0.1f & rotationbool == false)
    102.         {
    103.  
    104.            
    105.             gameObject.transform.rotation = new Quaternion(calculatedRotation.x, calculatedRotation.y, calculatedRotation.z, 1);
    106.  
    107.  
    108.         }
    109.         if (horizontal > -0.1 & rotationbool == false)
    110.         {
    111.  
    112.            
    113.             gameObject.transform.rotation = new Quaternion(calculatedRotation.x, calculatedRotation.y, calculatedRotation.z,1);
    114.  
    115.  
    116.         }
    117.  
    118.     }
    119.  
    120.     private static Quaternion GyroToUnity(Quaternion q)
    121.     {
    122.         return new Quaternion(q.x, q.y, -q.z, -q.w);
    123.  
    124.     }
    125.  
    126.  
    127. }