Search Unity

Hover Enter and Exit Script Error

Discussion in 'Scripting' started by Vampyr_Engel, Jul 29, 2015.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Hii I am getting a bit frustrated I managed to solve most of the errors in debugging as I got this of a youtube tutortial video ("UFPS plus vehicles")
    and modified it but I can't work out this out on this line:
    ( void OnTriggerEnter(Collider other) I keep getting this error on this line when it debugs "a namespace does not directly contain members such as fields or methods unity" What is wrong?
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. public class red_InteractVehicle : MonoBehaviour {
    4.  
    5.     //This Vehicle Information
    6.     Transform thisVehicle;
    7.     HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
    8.     HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
    9.     GameObject myCamera    ; //Camera for this vehicle
    10.     GameObject playerExitPosition; //Position to put the player on exiting vehicle
    11.     public GameObject myCanvas;//Basic control information
    12.     public float mouseSensitivityX = 250;
    13.     public float mouseSensitivityY = 250;
    14.     public float hoverSpeed = 20;
    15.     public float jumpForce = 1000f;
    16.     public float hoverForce = 65f;
    17.     public float hoverHeight = 3.5f;
    18.     //public void  EnterVehivle;
    19.     //public void exitvehicle;
    20.     public LayerMask groundedMask;
    21.     //This is the Player information
    22.     GameObject myPlayer;//UFPS Player game object
    23.     vp_FirstPersonController myController;//UFPS Player controller
    24.     vp_FPInput myInput;//UFPS Player Input
    25.  
    26.     //State of this vehicle, if we are in the vehicle or not
    27.     bool InThisVehicle=false;
    28.  
    29.  
    30.     void Start ()
    31.     {
    32.         //Set references to everything
    33.         thisVehicle = this.GetComponent<Transform>();
    34.         myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
    35.         myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
    36.         myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
    37.         playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
    38.  
    39.         //turn off Vehicle scripts and camera on start
    40.         myHoverAudio.enabled=false;
    41.         myHoverMotor.enabled=false;
    42.         myCamera.SetActive(false);
    43.         if(myCanvas)
    44.             myCanvas.SetActive(false);
    45.  
    46.     }
    47.  
    48.     //Exit vehicle keyboard input.  In fixed update so it doesn't move at computer speed
    49.     void FixedUpdate ()
    50.     {
    51.         if(InThisVehicle)
    52.         {
    53.             //Change the Input to whatever you want to use
    54.             if(Input.GetKeyDown(KeyCode.Z))
    55.                 ExitVehicle();
    56.         }
    57.  
    58.     }
    59.     public void ExitVehicle()
    60.     {
    61.         myPlayer.SetActive(true);//turn on the player
    62.         myController.SetPosition(playerExitPosition.transform.position);//move the player
    63.         myInput.enabled=true;//enable the player's input
    64.         myHoverMotor.enabled=false;//turn off vehicle stuff
    65.         myHoverAudio.enabled=false;//turn off vehicle stuff
    66.         myCamera.SetActive(false);//turn off vehicle stuff
    67.         InThisVehicle=false;//We aren't in the vehicle anymore
    68.         if(myCanvas)
    69.             myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
    70.         public class FirstPersonController : MonoBehaviour {
    71.      
    72.             // public vars
    73.             public float mouseSensitivityX = 250;
    74.             public float mouseSensitivityY = 250;
    75.             public float walkSpeed = 8;
    76.             public float jumpForce = 220;
    77.             public LayerMask groundedMask;
    78.      
    79.             // System vars
    80.             bool grounded;
    81.             Vector3 moveAmount;
    82.             Vector3 smoothMoveVelocity;
    83.             float verticalLookRotation;
    84.             Transform cameraTransform;
    85.             Rigidbody rigidbody;
    86.      
    87.      
    88.             void Awake() {
    89.                 Screen.lockCursor = true;
    90.                 cameraTransform = Camera.main.transform;
    91.                 rigidbody = GetComponent<Rigidbody> ();
    92.             }
    93.      
    94.             void Update() {
    95.          
    96.                 // Look rotation:
    97.                 transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    98.                 verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    99.                 verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
    100.                 cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    101.          
    102.                 // Calculate movement:
    103.                 float inputX = Input.GetAxisRaw("Horizontal");
    104.                 float inputY = Input.GetAxisRaw("Vertical");
    105.          
    106.                 Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    107.                 Vector3 targetMoveAmount = moveDir * walkSpeed;
    108.                 moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    109.          
    110.                 // Jump
    111.                 if (Input.GetButtonDown("Jump")) {
    112.                     if (grounded) {
    113.                         rigidbody.AddForce(transform.up * jumpForce);
    114.                     }
    115.                 }
    116.             }
    117.                 // Grounded check
    118.                 //Ray ray = new Ray(transform.position, -transform.up);
    119.                 //RaycastHit hit;
    120.          
    121.                 //if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    122.                     //grounded = true;
    123.                 //}
    124.                 //else {
    125.                 //    grounded = false;
    126.                 //}
    127.          
    128.             //}
    129.      
    130.             void FixedUpdate() {
    131.                 // Apply movement to rigidbody
    132.                 Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    133.                 rigidbody.MovePosition(rigidbody.position + localMove);
    134.             }
    135.  
    136.     }
    137.     public void EnterVehicle()
    138.     {
    139.         myPlayer.SetActive(false);//turn off the player
    140.         myHoverMotor.enabled=true;//turn on vehicle stuff
    141.         myHoverAudio.enabled=true;//turn on vehicle stuff
    142.         myCamera.SetActive(true);//turn on vehicle stuff
    143.         InThisVehicle=true;//We are in the vehicle now
    144.         if(myCanvas)
    145.                 myCanvas.
    146.     SetActive(true);RequireComponent (typeof (GravityBody))]
    147.  
    148.                 // System vars
    149.                 bool grounded;
    150.                 Vector3 moveAmount;
    151.                 Vector3 smoothMoveVelocity;
    152.                 float verticalLookRotation;
    153.                 Transform cameraTransform;
    154.                 Rigidbody hoverRigidbody;
    155.                                   }
    156.          
    157.          
    158.                 void Awake() {
    159.                     Screen.lockCursor = true;
    160.                     cameraTransform = Camera.main.transform;
    161.                     hoverRigidbody = GetComponent<Rigidbody> ();
    162.                 }
    163.          
    164.                 void Update() {
    165.              
    166.                     // Look rotation:
    167.                     transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    168.                     verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    169.                     verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
    170.                     cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    171.              
    172.                     // Calculate movement:
    173.                     float inputX = Input.GetAxisRaw("Horizontal");
    174.                     float inputY = Input.GetAxisRaw("Vertical");
    175.              
    176.                     //Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    177.                     //Vector3 targetMoveAmount = moveDir * hoverSpeed;
    178.                     //moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    179.              
    180.              
    181.                     // Grounded check
    182.                     //Ray ray = new Ray(transform.position, -transform.up);
    183.                     //RaycastHit hit;
    184.              
    185.                     if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    186.                         grounded = true;
    187.                     }
    188.                     else {
    189.                         grounded = false;
    190.                     }
    191.              
    192.                 }
    193.          
    194.                 void FixedUpdate() {
    195.                     // Apply movement to rigidbody
    196.                     Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    197.                     hoverRigidbody.MovePosition(hoverRigidbody.position + localMove);// Jump
    198.                     if (Input.GetButtonDown("Jump")) {
    199.                         if (grounded) {
    200.                             hoverRigidbody.AddForce(transform.up * jumpForce);
    201.                         }
    202.                     }
    203.              
    204.                 }
    205.             }
    206.      
    207.  
    208.  
    209.  
    210.     //Make sure your trigger collider is big enough for the player to interact
    211.     //Also, make sure the Exit position is outside the trigger area
    212.     void OnTriggerEnter(Collider other)
    213.     {
    214.         //if a player enters trigger and the player isn't already in the vehicle
    215.         //just in case there are multiple players
    216.         if(other.gameObject.tag=="Player"&&InThisVehicle==false)
    217.         {
    218.             myPlayer=other.gameObject;//Get the player
    219.             myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
    220.             myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
    221.             EnterVehicle();//Run the EnterVehicle function
    222.         }
    223.     }
     
    Last edited: Aug 1, 2015
  2. mlnDb10wN

    mlnDb10wN

    Joined:
    Sep 29, 2011
    Posts:
    8
    Try removing a } bracket before

    "//Make sure your trigger collider is big enough for the player to interact
    //Also, make sure the Exit position is outside the trigger area
    void OnTriggerEnter(Collider other)"

    Also not sure if this is all in one script but why the multiple Update() functions etc.?
     
  3. mlnDb10wN

    mlnDb10wN

    Joined:
    Sep 29, 2011
    Posts:
    8
    void FixedUpdate() {
    // Apply movement to rigidbody
    Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    hoverRigidbody.MovePosition(hoverRigidbody.position + localMove);// Jump
    if (Input.GetButtonDown("Jump")) {
    if (grounded) {
    hoverRigidbody.AddForce(transform.up * jumpForce);
    }
    }

    }

    } <<<< One too many brackets.
     
  4. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Thank you
     
  5. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Unfortunately getting another "a namespace does not directly contain members such as fields or methods unity" around this code
    Code (CSharp):
    1.  
    2.                  //Make sure your trigger collider is big enough for the player to interact
    3.                                   //Also, make sure the Exit position is outside the trigger area
    4.                                   void OnTriggerEnter (Collider other);}
    5.  
    6.             //if a player enters trigger and the player isn't already in the vehicle
    7.             //just in case there are multiple players
    8.             if (other.gameObject.tag=="Player"&&InThisVehicle==false)
    9.        
    10.                 myPlayer=other.gameObject;//Get the player
    11.                 FirstPersonController=myPlayer.GetComponent<FirstPersonController>();//Get the controller
    12.                 myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
    13.                 EnterVehicle();//Run the EnterVehicle function
    14.          
    Epsecailly on this line help
     
    Last edited: Aug 1, 2015
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    [code ][/code ] tags are required, as indicated by the sticky at the top of every forum here. Most of the better programmers here aren't even going to bother trying to read this without them.
     
    mlnDb10wN likes this.
  7. mlnDb10wN

    mlnDb10wN

    Joined:
    Sep 29, 2011
    Posts:
    8
    You should post the full source in [code ][/code ] as Lysander said. This is painful to read. I think it is another syntax issue.
     
  8. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Code (CSharp):
    1.  
    2. using System.Collections;
    3.  
    4. public class red_InteractVehicle : MonoBehaviour {
    5.  
    6. //This Vehicle Information
    7. Transform thisVehicle;
    8. HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
    9. HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
    10. GameObject myCamera ; //Camera for this vehicle
    11. GameObject playerExitPosition; //Position to put the player on exiting vehicle
    12. public GameObject myCanvas;//Basic control information
    13. public float mouseSensitivityX = 250;
    14. public float mouseSensitivityY = 250;
    15. public float hoverSpeed = 20;
    16. public float jumpForce = 1000f;
    17. public float hoverForce = 65f;
    18. public float hoverHeight = 3.5f;
    19. //public void EnterVehivle;
    20. //public void exitvehicle;
    21. public LayerMask groundedMask;
    22. //This is the Player information
    23. GameObject myPlayer;//UFPS Player game object
    24. vp_FirstPersonController myController;//UFPS Player controller
    25. vp_FPInput myInput;//UFPS Player Input
    26.  
    27. //State of this vehicle, if we are in the vehicle or not
    28. bool InThisVehicle=false;
    29.  
    30.  
    31. void Start ()
    32. {
    33. //Set references to everything
    34. thisVehicle = this.GetComponent<Transform>();
    35. myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
    36. myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
    37. myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
    38. playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
    39.  
    40. //turn off Vehicle scripts and camera on start
    41. myHoverAudio.enabled=false;
    42. myHoverMotor.enabled=false;
    43. myCamera.SetActive(false);
    44. if(myCanvas)
    45. myCanvas.SetActive(false);
    46.  
    47. }
    48.  
    49. //Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
    50. void FixedUpdate ()
    51. {
    52. if(InThisVehicle)
    53. {
    54. //Change the Input to whatever you want to use
    55. if(Input.GetKeyDown(KeyCode.Z))
    56. ExitVehicle();
    57. }
    58.  
    59. }
    60. public void ExitVehicle()
    61. {
    62. myPlayer.SetActive(true);//turn on the player
    63. myController.SetPosition(playerExitPosition.transform.position);//move the player
    64. myInput.enabled=true;//enable the player's input
    65. myHoverMotor.enabled=false;//turn off vehicle stuff
    66. myHoverAudio.enabled=false;//turn off vehicle stuff
    67. myCamera.SetActive(false);//turn off vehicle stuff
    68. InThisVehicle=false;//We aren't in the vehicle anymore
    69. if(myCanvas)
    70. myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
    71. public class FirstPersonController : MonoBehaviour {
    72.  
    73. // public vars
    74. public float mouseSensitivityX = 250;
    75. public float mouseSensitivityY = 250;
    76. public float walkSpeed = 8;
    77. public float jumpForce = 220;
    78. public LayerMask groundedMask;
    79.  
    80. // System vars
    81. bool grounded;
    82. Vector3 moveAmount;
    83. Vector3 smoothMoveVelocity;
    84. float verticalLookRotation;
    85. Transform cameraTransform;
    86. Rigidbody rigidbody;
    87.  
    88.  
    89. void Awake() {
    90. Screen.lockCursor = true;
    91. cameraTransform = Camera.main.transform;
    92. rigidbody = GetComponent<Rigidbody> ();
    93. }
    94.  
    95. void Update() {
    96.  
    97. // Look rotation:
    98. transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    99. verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    100. verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
    101. cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    102.  
    103. // Calculate movement:
    104. float inputX = Input.GetAxisRaw("Horizontal");
    105. float inputY = Input.GetAxisRaw("Vertical");
    106.  
    107. Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    108. Vector3 targetMoveAmount = moveDir * walkSpeed;
    109. moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    110.  
    111. // Jump
    112. if (Input.GetButtonDown("Jump")) {
    113. if (grounded) {
    114. rigidbody.AddForce(transform.up * jumpForce);
    115. }
    116. }
    117. }
    118. // Grounded check
    119. //Ray ray = new Ray(transform.position, -transform.up);
    120. //RaycastHit hit;
    121.  
    122. //if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    123. //grounded = true;
    124. //}
    125. //else {
    126. // grounded = false;
    127. //}
    128.  
    129. //}
    130.  
    131. void FixedUpdate() {
    132. // Apply movement to rigidbody
    133. Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    134. rigidbody.MovePosition(rigidbody.position + localMove);
    135. }
    136.  
    137. }
    138. public void EnterVehicle()
    139. {
    140. myPlayer.SetActive(false);//turn off the player
    141. myHoverMotor.enabled=true;//turn on vehicle stuff
    142. myHoverAudio.enabled=true;//turn on vehicle stuff
    143. myCamera.SetActive(true);//turn on vehicle stuff
    144. InThisVehicle=true;//We are in the vehicle now
    145. if(myCanvas)
    146. myCanvas.
    147. SetActive(true);RequireComponent (typeof (GravityBody))]
    148.  
    149. // System vars
    150. bool grounded;
    151. Vector3 moveAmount;
    152. Vector3 smoothMoveVelocity;
    153. float verticalLookRotation;
    154. Transform cameraTransform;
    155. Rigidbody hoverRigidbody;
    156. }
    157.  
    158.  
    159. void Awake() {
    160. Screen.lockCursor = true;
    161. cameraTransform = Camera.main.transform;
    162. hoverRigidbody = GetComponent<Rigidbody> ();
    163. }
    164.  
    165. void Update() {
    166.  
    167. // Look rotation:
    168. transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    169. verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    170. verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
    171. cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    172.  
    173. // Calculate movement:
    174. float inputX = Input.GetAxisRaw("Horizontal");
    175. float inputY = Input.GetAxisRaw("Vertical");
    176.  
    177. //Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    178. //Vector3 targetMoveAmount = moveDir * hoverSpeed;
    179. //moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    180.  
    181.  
    182. // Grounded check
    183. //Ray ray = new Ray(transform.position, -transform.up);
    184. //RaycastHit hit;
    185.  
    186. if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    187. grounded = true;
    188. }
    189. else {
    190. grounded = false;
    191. }
    192.  
    193. }
    194.  
    195. void FixedUpdate() {
    196. // Apply movement to rigidbody
    197. Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    198. hoverRigidbody.MovePosition(hoverRigidbody.position + localMove);// Jump
    199. if (Input.GetButtonDown("Jump")) {
    200. if (grounded) {
    201. hoverRigidbody.AddForce(transform.up * jumpForce);
    202. }
    203. }
    204.  
    205. }
    206. }
    207.  
    208.  
    209.  
    210.  
    211. //Make sure your trigger collider is big enough for the player to interact
    212. //Also, make sure the Exit position is outside the trigger area
    213. void OnTriggerEnter(Collider other)
    214. {
    215. //if a player enters trigger and the player isn't already in the vehicle
    216. //just in case there are multiple players
    217. if(other.gameObject.tag=="Player"&&InThisVehicle==false)
    218. {
    219. myPlayer=other.gameObject;//Get the player
    220. myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
    221. myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
    222. EnterVehicle();//Run the EnterVehicle function
    223. }
    224. }
    225.  
    226.  
    227.  
    There you go happy?
     
  9. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Not at all. Does posting an unreadable wall of test with no formatting seem like the kind of thing that would make a person happy? You do know that we're trying to help you, right? We can't just copy this and put it in our own editors, because the classes you have don't exist for us- it would be red errors everywhere.

    Just like the last time, you might be better served to check immediately before the function in question and see if you have an extra } that's cutting off the rest of the class. That's all I can say though, with this.

    Best of luck.
     
    Last edited: Aug 1, 2015
  10. djiango

    djiango

    Joined:
    Jul 19, 2015
    Posts:
    2
    Last edited: Aug 1, 2015
  11. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    OK Thank you I will
     
  12. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Code (CSharp):
    1. using System.Collections;
    2. public class red_InteractVehicle : MonoBehaviour {
    3.    
    4.     //This Vehicle Information
    5.     Transform thisVehicle;
    6.     HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
    7.     HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
    8.     GameObject myCamera ; //Camera for this vehicle
    9.     GameObject playerExitPosition; //Position to put the player on exiting vehicle
    10.     public GameObject myCanvas;//Basic control information
    11.     public float mouseSensitivityX = 250;
    12.     public float mouseSensitivityY = 250;
    13.     public float hoverSpeed = 20;
    14.     public float jumpForce = 1000f;
    15.     public float hoverForce = 65f;
    16.     public float hoverHeight = 3.5f;
    17.     //public void EnterVehivle;
    18.     //public void exitvehicle;
    19.     public LayerMask groundedMask;
    20.     //This is the Player information
    21.     GameObject myPlayer;//UFPS Player game object
    22.     vp_FirstPersonController myController;//UFPS Player controller
    23.     vp_FPInput myInput;//UFPS Player Input
    24.    
    25.     //State of this vehicle, if we are in the vehicle or not
    26.     bool InThisVehicle=false;
    27.    
    28.    
    29.     void Start ()
    30.     {
    31.         //Set references to everything
    32.         thisVehicle = this.GetComponent<Transform>();
    33.         myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
    34.         myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
    35.         myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
    36.         playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
    37.        
    38.         //turn off Vehicle scripts and camera on start
    39.         myHoverAudio.enabled=false;
    40.         myHoverMotor.enabled=false;
    41.         myCamera.SetActive(false);
    42.         if(myCanvas)
    43.             myCanvas.SetActive(false);
    44.        
    45.     }
    46.    
    47.     //Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
    48.     void FixedUpdate ()
    49.     {
    50.         if(InThisVehicle)
    51.         {
    52.             //Change the Input to whatever you want to use
    53.             if(Input.GetKeyDown(KeyCode.Z))
    54.                 ExitVehicle();
    55.         }
    56.        
    57.     }
    58.     public void ExitVehicle()
    59.     {
    60.         myPlayer.SetActive(true);//turn on the player
    61.         myController.SetPosition(playerExitPosition.transform.position);//move the player
    62.         myInput.enabled=true;//enable the player's input
    63.         myHoverMotor.enabled=false;//turn off vehicle stuff
    64.         myHoverAudio.enabled=false;//turn off vehicle stuff
    65.         myCamera.SetActive(false);//turn off vehicle stuff
    66.         InThisVehicle=false;//We aren't in the vehicle anymore
    67.         if(myCanvas)
    68.             myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
    69.     public class FirstPersonController : MonoBehaviour {
    70.        
    71.         // public vars
    72.         public float mouseSensitivityX = 250;
    73.         public float mouseSensitivityY = 250;
    74.         public float walkSpeed = 8;
    75.         public float jumpForce = 220;
    76.         public LayerMask groundedMask;
    77.        
    78.         // System vars
    79.         bool grounded;
    80.         Vector3 moveAmount;
    81.         Vector3 smoothMoveVelocity;
    82.         float verticalLookRotation;
    83.         Transform cameraTransform;
    84.         Rigidbody rigidbody;
    85.        
    86.        
    87.         void Awake() {
    88.             Screen.lockCursor = true;
    89.             cameraTransform = Camera.main.transform;
    90.             rigidbody = GetComponent<Rigidbody> ();
    91.         }
    92.        
    93.         void Update() {
    94.            
    95.             // Look rotation:
    96.             transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    97.             verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    98.             verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
    99.             cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    100.            
    101.             // Calculate movement:
    102.             float inputX = Input.GetAxisRaw("Horizontal");
    103.             float inputY = Input.GetAxisRaw("Vertical");
    104.            
    105.             Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    106.             Vector3 targetMoveAmount = moveDir * walkSpeed;
    107.             moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    108.            
    109.             // Jump
    110.             if (Input.GetButtonDown("Jump")) {
    111.                 if (grounded) {
    112.                     rigidbody.AddForce(transform.up * jumpForce);
    113.                 }
    114.             }
    115.         }
    116.        
    117.        
    118.        
    119.         // Apply movement to rigidbody
    120.         Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    121.         rigidbody.MovePosition rigidbody;position localMove;
    122.     }
    123.    
    124. }
    125. public void; EnterVehicle(){
    126.     {
    127.         myPlayer.SetActive(false);//turn off the player
    128.         myHoverMotor.enabled=true;//turn on vehicle stuff
    129.         myHoverAudio.enabled=true;//turn on vehicle stuff
    130.         myCamera.SetActive(true);//turn on vehicle stuff
    131.         InThisVehicle=true;//We are in the vehicle now
    132.         if(myCanvas)
    133.             myCanvas.
    134.         SetActive(true);RequireComponent (typeof (GravityBody))]
    135.        
    136.         // System vars
    137.         bool grounded;
    138.         Vector3 moveAmount;
    139.         Vector3 smoothMoveVelocity;
    140.         float verticalLookRotation;
    141.         Transform cameraTransform;
    142.         Rigidbody hoverRigidbody;
    143.     }
    144.    
    145.    
    146.     void Awake() {
    147.         Screen.lockCursor = true;
    148.         cameraTransform = Camera.main.transform;
    149.         hoverRigidbody = GetComponent<Rigidbody> ();
    150.     }
    151.    
    152.     void Update() {
    153.        
    154.         // Look rotation:
    155.         transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    156.         verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    157.         verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
    158.         cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    159.        
    160.         // Calculate movement:
    161.         float inputX = Input.GetAxisRaw("Horizontal");
    162.         float inputY = Input.GetAxisRaw("Vertical");
    163.        
    164.         vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    165.         Vector3 targetMoveAmount = moveDir * hoverSpeed;
    166.         moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    167.        
    168.        
    169.         // Grounded check
    170.         //Ray ray = new Ray(transform.position, -transform.up);
    171.         //RaycastHit hit;
    172.        
    173.         //if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    174.             //    grounded = true;
    175.             //}
    176.         //else {
    177.             //    grounded = false;
    178.             //}
    179.        
    180.        
    181.        
    182.         void FixedUpdate() {
    183.             // Apply movement to rigidbody
    184.             Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    185.             hoverRigidbody.MovePosition(hoverRigidbody.position + localMove);// Jump
    186.             if (Input.GetButtonDown("Jump")) {
    187.                 if (grounded) {
    188.                     hoverRigidbody.AddForce(transform.up * jumpForce);
    189.                 }
    190.             }
    191.            
    192.            
    193.            
    194.            
    195.            
    196.             //Make sure your trigger collider is big enough for the player to interact
    197.             //Also, make sure the Exit position is outside the trigger area
    198.             void OnTriggerEnter(Collider other)
    199.             {
    200.                 //if a player enters trigger and the player isn't already in the vehicle
    201.                 //just in case there are multiple players
    202.                 if(other.gameObject.tag=="Player"&&InThisVehicle==false)
    203.                 {
    204.                     myPlayer=other.gameObject;//Get the player
    205.                     myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
    206.                     myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
    207.                     EnterVehicle();//Run the EnterVehicle function
    208.                 }
    209.             }
     
  13. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Well I am still having problems I seem to have errors according codeGenerators
    • Error between line 2 and 10
    • Error between line 69 and 72
    • Error between line 76 and 125
    Still need to help on this I am afriad

    Code (CSharp):
    1. using System.Collections;
    2. public class red_InteractVehicle : MonoBehaviour {
    3.    
    4.     //This Vehicle Information
    5.     Transform thisVehicle;
    6.     HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
    7.     HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
    8.     GameObject myCamera ; //Camera for this vehicle
    9.     GameObject playerExitPosition; //Position to put the player on exiting vehicle
    10.     public GameObject myCanvas;//Basic control information
    11.     public float mouseSensitivityX = 250;
    12.     public float mouseSensitivityY = 250;
    13.     public float hoverSpeed = 20;
    14.     public float jumpForce = 1000f;
    15.     public float hoverForce = 65f;
    16.     public float hoverHeight = 3.5f;
    17.     //public void EnterVehivle;
    18.     //public void exitvehicle;
    19.     public LayerMask groundedMask;
    20.     //This is the Player information
    21.     GameObject myPlayer;//UFPS Player game object
    22.     vp_FirstPersonController myController;//UFPS Player controller
    23.     vp_FPInput myInput;//UFPS Player Input
    24.    
    25.     //State of this vehicle, if we are in the vehicle or not
    26.     bool InThisVehicle=false;
    27.    
    28.    
    29.     void Start ()
    30.     {
    31.         //Set references to everything
    32.         thisVehicle = this.GetComponent<Transform>();
    33.         myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
    34.         myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
    35.         myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
    36.         playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
    37.        
    38.         //turn off Vehicle scripts and camera on start
    39.         myHoverAudio.enabled=false;
    40.         myHoverMotor.enabled=false;
    41.         myCamera.SetActive(false);
    42.         if(myCanvas)
    43.             myCanvas.SetActive(false);
    44.        
    45.     }
    46.    
    47.     //Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
    48.     void FixedUpdate ()
    49.     {
    50.         if(InThisVehicle)
    51.         {
    52.             //Change the Input to whatever you want to use
    53.             if(Input.GetKeyDown(KeyCode.Z))
    54.                 ExitVehicle();
    55.         }
    56.        
    57.     }
    58.     public void ExitVehicle()
    59.     {
    60.         myPlayer.SetActive(true);//turn on the player
    61.         myController.SetPosition(playerExitPosition.transform.position);//move the player
    62.         myInput.enabled=true;//enable the player's input
    63.         myHoverMotor.enabled=false;//turn off vehicle stuff
    64.         myHoverAudio.enabled=false;//turn off vehicle stuff
    65.         myCamera.SetActive(false);//turn off vehicle stuff
    66.         InThisVehicle=false;//We aren't in the vehicle anymore
    67.         if(myCanvas)
    68.             myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
    69.     public class FirstPersonController : MonoBehaviour {
    70.        
    71.         // public vars
    72.         public float mouseSensitivityX = 250;
    73.         public float mouseSensitivityY = 250;
    74.         public float walkSpeed = 8;
    75.         public float jumpForce = 220;
    76.         public LayerMask groundedMask;
    77.        
    78.         // System vars
    79.         bool grounded;
    80.         Vector3 moveAmount;
    81.         Vector3 smoothMoveVelocity;
    82.         float verticalLookRotation;
    83.         Transform cameraTransform;
    84.         Rigidbody rigidbody;
    85.        
    86.        
    87.         void Awake() {
    88.             Screen.lockCursor = true;
    89.             cameraTransform = Camera.main.transform;
    90.             rigidbody = GetComponent<Rigidbody> ();
    91.         }
    92.        
    93.         void Update() {
    94.            
    95.             // Look rotation:
    96.             transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    97.             verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    98.             verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
    99.             cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    100.            
    101.             // Calculate movement:
    102.             float inputX = Input.GetAxisRaw("Horizontal");
    103.             float inputY = Input.GetAxisRaw("Vertical");
    104.            
    105.             Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    106.             Vector3 targetMoveAmount = moveDir * walkSpeed;
    107.             moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    108.            
    109.             // Jump
    110.             if (Input.GetButtonDown("Jump")) {
    111.                 if (grounded) {
    112.                     rigidbody.AddForce(transform.up * jumpForce);
    113.                 }
    114.             }
    115.         }
    116.        
    117.        
    118.        
    119.         // Apply movement to rigidbody
    120.         Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    121.         rigidbody.MovePosition rigidbody;position localMove;
    122.     }
    123.    
    124. }
    125. public void; EnterVehicle()
    126. {
    127.     myPlayer.SetActive(false);//turn off the player
    128.     myHoverMotor.enabled=true;//turn on vehicle stuff
    129.     myHoverAudio.enabled=true;//turn on vehicle stuff
    130.     myCamera.SetActive(true);//turn on vehicle stuff
    131.     InThisVehicle=true;//We are in the vehicle now
    132.     if(myCanvas)
    133.         myCanvas.
    134.     SetActive(true);RequireComponent (typeof (GravityBody))]
    135.    
    136.     // System vars
    137.     bool grounded;
    138.     Vector3 moveAmount;
    139.     Vector3 smoothMoveVelocity;
    140.     float verticalLookRotation;
    141.     Transform cameraTransform;
    142.     Rigidbody hoverRigidbody;
    143. }
    144.  
    145.  
    146. void Awake() {
    147.     Screen.lockCursor = true;
    148.     cameraTransform = Camera.main.transform;
    149.     hoverRigidbody = GetComponent<Rigidbody> ();
    150. }
    151.  
    152. void Update() {
    153.    
    154.     // Look rotation:
    155.     transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
    156.     verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
    157.     verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
    158.     cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
    159.    
    160.     // Calculate movement:
    161.     float inputX = Input.GetAxisRaw("Horizontal");
    162.     float inputY = Input.GetAxisRaw("Vertical");
    163.    
    164.     //vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
    165.     //Vector3 targetMoveAmount = moveDir * hoverSpeed;
    166.     //moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
    167.    
    168.    
    169.     // Grounded check
    170.     //Ray ray = new Ray(transform.position, -transform.up);
    171.     //RaycastHit hit;
    172.    
    173.     //if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
    174.         //    grounded = true;
    175.         //}
    176.     //else {
    177.         //    grounded = false;
    178.         //}
    179.    
    180.    
    181.    
    182.     void FixedUpdate() {
    183.         // Apply movement to rigidbody
    184.         Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
    185.         hoverRigidbody.MovePosition(hoverRigidbody.position + localMove);// Jump
    186.         if (Input.GetButtonDown("Jump")) {
    187.             if (grounded) {
    188.                 hoverRigidbody.AddForce(transform.up * jumpForce);
    189.             }
    190.            
    191.            
    192.            
    193.            
    194.            
    195.            
    196.             //Make sure your trigger collider is big enough for the player to interact
    197.             //Also, make sure the Exit position is outside the trigger area
    198.             void OnTriggerEnter(Collider other)
    199.             {
    200.                 //if a player enters trigger and the player isn't already in the vehicle
    201.                 //just in case there are multiple players
    202.                 if(other.gameObject.tag=="Player"&&InThisVehicle==false)
    203.                 {
    204.                     myPlayer=other.gameObject;//Get the player
    205.                     myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
    206.                     myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
    207.                     EnterVehicle();//Run the EnterVehicle function
    208.                 }
    209.             }
     
    Last edited: Aug 3, 2015
  14. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
  15. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    I wonder if a "include script" command would be better?