Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Move player to vehicle

Discussion in 'Scripting' started by IbrahimAlkaber, Mar 13, 2020.

  1. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    Hi, How can I move a player to his car automatically when he enters the room if I have 2 prefabs ( player and vehicle ).
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I would definitely start with a few tutorials off google. Try searching for:

    unity get in and out of vehicle
     
  3. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    @Kurt-Dekker Hi,
    I did the requirements but the rest is to put the player in his car when he enters the rum

    Code (CSharp):
    1.  
    2.     [HideInInspector] public GameObject m_Player { get; set; }
    3.     [HideInInspector] public GameObject m_Vehicle { get; set; }
    4.  
    5. public void SpawnPlayer(Vector3 Position, Quaternion Rotation)
    6.     {
    7.         if (PlayerPrefab == null)
    8.         {
    9.             Debug.Log("Player Prefabs I was not assigned yet!");
    10.             return;
    11.         }
    12.         //Sure of have just only player on room
    13.         if (m_Player != null)
    14.         {
    15.             if (m_Player.GetComponent<bl_PlayerCar>().isPlayerInsideVehicle)
    16.             {
    17.                 m_Player.GetComponent<bl_PlayerCar>().ExitVehicle();
    18.             }
    19.             NetworkDestroy(m_Player);
    20.         }
    21.  
    22.  
    23.             Vector3 fp = Position + Vector3.up;
    24.         m_Player = PhotonNetwork.Instantiate(PlayerPrefab.name, fp, Rotation, 0);
    25.         m_Vehicle = PhotonNetwork.Instantiate(CarPrefab.name, fp , Rotation, 0);
    26.            m_Vehicle.GetComponent<bl_VehicleManager>().OnEnter();
    27.  
    28.  
    29.         m_ViewID = m_Player.GetViewID();
    30.         bl_EventHandler.OnLocalPlayerSpawn(m_Player);
    31.         if (RoomCamera != null)
    32.         {
    33.             RoomCamera.SetActive(false);
    34.         }
    35.         bl_CoopUtils.LockCursor(true);
    36.         isPlaying = true;
    37.     }
    38.  
    39.  
    40.  
    41. //vehicle enter
    42.  
    43.     public void OnEnter()
    44.     {
    45.         if (view.Owner == null || view.Owner.UserId != PhotonNetwork.LocalPlayer.UserId)
    46.         {
    47.             view.RequestOwnership();
    48.         }
    49.         bl_EventHandler.OnLocalPlayerVehicle(true, m_VehicleType);
    50.         foreach (MonoBehaviour m in VehicleScripts)
    51.         {
    52.             m.enabled = true;
    53.         }
    54.         if(m_VehicleType == VehicleType.Jet) { JetScript.Reset(); }
    55.  
    56.         if (AnimatedEnter) { bl_VehicleCamera.Instance.SetCarTarget(transform, Player.GetComponent<bl_PlayerPhoton>().PlayerCamera.transform); }
    57.         else { bl_VehicleCamera.Instance.SetCarTarget(transform); }
    58.  
    59.         Player.transform.parent = PlayerHolder;
    60.         Player.transform.localPosition = DriverPosition;
    61.         Player.transform.localEulerAngles = DriverRotation;
    62.  
    63.         Player.GetComponent<bl_PlayerCar>().OnEnterLocal(this);
    64.         LocalInVehicle = true;
    65.         LocalOnTrigger = false;
    66.         VehicleUI.SetEnterUI(false);
    67.         VehicleUI.OnEnter(m_VehicleType);
    68.         VehicleUI.SetHealth(Health);
    69.         LocalPlayerView.RPC("NetworkCarEvent", RpcTarget.OthersBuffered, true, m_VehicleType, photonView.ViewID, PlayerVisibleInside);
    70.         view.RPC("InAndOutEvent", RpcTarget.OthersBuffered, true);
    71.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    1. what do you expect and why
    2. what is happening (errors, exact text, where and when the error is),
    3. where you think the problem might be. (line number)

    Are you aware that this code uses the deprecated UNet networking layer? (I think?)

    https://docs.unity3d.com/Manual/UNet.html
     
  5. IbrahimAlkaber

    IbrahimAlkaber

    Joined:
    Dec 9, 2019
    Posts:
    53
    @Kurt-Dekker I think that when a player enters a room and descends, he does not find the type of vehicle he is riding and an error appears
    Code (CSharp):
    1.            m_Vehicle.GetComponent<bl_VehicleManager>().OnEnter();
    2.  
    the error is in a line 56

    --
    //Vehicle camera script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bl_VehicleCamera : MonoBehaviour
    5. {
    6.     public AnimationCurve TransitionCurve;
    7.     public float TransitionDuration = 0.5f;
    8.     public float smoothing = 6f;
    9.  
    10.     private Transform lookAtTarget;
    11.     private Transform positionTarget;
    12.  
    13.     private bool Hold = false;
    14.     private bool Interrup = false;
    15.     private float CameraTargetDistance = 1;
    16.  
    17.     /// <summary>
    18.     ///
    19.     /// </summary>
    20.     private void Awake()
    21.     {
    22.         //cache singleton
    23.         bl_VehicleCamera.Instance.Disable();
    24.     }
    25.  
    26.     /// <summary>
    27.     ///
    28.     /// </summary>
    29.     private void FixedUpdate()
    30.     {
    31.         if (lookAtTarget == null || Hold)
    32.             return;
    33.  
    34.         UpdateCamera();
    35.         DetectCollision();
    36.     }
    37.  
    38.     /// <summary>
    39.     ///
    40.     /// </summary>
    41.     private void UpdateCamera()
    42.     {
    43.         if (Interrup)
    44.             return;
    45.  
    46.         transform.position = Vector3.Lerp(transform.position, positionTarget.position, Time.deltaTime * smoothing);
    47.         transform.LookAt(lookAtTarget);
    48.     }
    49.  
    50.     void DetectCollision()
    51.     {
    52.         //if there a obstacle detected
    53.         Interrup = (Physics.CheckSphere(transform.position, 0.5f));
    54.         if (Interrup)
    55.         {
    56.             transform.LookAt(lookAtTarget);
    57.             if (Vector3.Distance(transform.position, lookAtTarget.position) >= CameraTargetDistance) { Interrup = false; }
    58.         }
    59.     }
    60.  
    61.     private void OnDrawGizmos()
    62.     {
    63.         Gizmos.DrawWireSphere(transform.position , 0.5f);
    64.     }
    65.  
    66.     public void SetCarTarget(Transform car)
    67.     {
    68.         GetRig(car);
    69.         gameObject.SetActive(true);
    70.     }
    71.  
    72.     public void SetCarTarget(Transform car,Transform from)
    73.     {
    74.         Hold = true;
    75.         bool f = GetRig(car);
    76.         CameraTargetDistance = Vector3.Distance(lookAtTarget.position, positionTarget.position);
    77.         gameObject.SetActive(true);
    78.         if (f)
    79.         {
    80.             StartCoroutine(Transition(from));
    81.         }
    82.     }
    83.  
    84.     /// <summary>
    85.     ///
    86.     /// </summary>
    87.     public void Disable()
    88.     {
    89.         StopAllCoroutines();
    90.         lookAtTarget = null;
    91.         positionTarget = null;
    92.         gameObject.SetActive(false);
    93.         Hold = false;
    94.     }
    95.  
    96.     bool GetRig(Transform car)
    97.     {
    98.         bl_VehicleCameraRig vr = car.GetComponentInChildren<bl_VehicleCameraRig>();
    99.         if (vr == null) { Debug.LogWarning("Can't found camera rig references on child's."); return false; }
    100.  
    101.         lookAtTarget = vr.LookAtTarget;
    102.         positionTarget = vr.CameraPosition;
    103.         return true;
    104.     }
    105.  
    106.     IEnumerator Transition(Transform From)
    107.     {
    108.         float d = 0;
    109.         Vector3 po = From.position;
    110.         Quaternion ro = From.rotation;
    111.         transform.position = po;
    112.         transform.rotation = ro;
    113.         positionTarget.LookAt(lookAtTarget);
    114.         float curve = 0;
    115.  
    116.         while(d < 1)
    117.         {
    118.             d += Time.deltaTime / TransitionDuration;
    119.             curve = TransitionCurve.Evaluate(d);
    120.             transform.position = Vector3.Lerp(po, positionTarget.position, curve);
    121.             transform.rotation = Quaternion.Slerp(ro, positionTarget.rotation, curve);
    122.             yield return null;
    123.         }
    124.         Hold = false;
    125.     }
    126.  
    127.     private static bl_VehicleCamera _instance;
    128.     public static bl_VehicleCamera Instance
    129.     {
    130.         get
    131.         {
    132.             if (_instance == null) { _instance = FindObjectOfType<bl_VehicleCamera>(); }
    133.             return _instance;
    134.         }
    135.     }
    136. }
     
    Last edited: Mar 14, 2020