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?
using System.Collections;
public class red_InteractVehicle : MonoBehaviour {
//This Vehicle Information
Transform thisVehicle;
HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
GameObject myCamera ; //Camera for this vehicle
GameObject playerExitPosition; //Position to put the player on exiting vehicle
public GameObject myCanvas;//Basic control information
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float hoverSpeed = 20;
public float jumpForce = 1000f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
//public void EnterVehivle;
//public void exitvehicle;
public LayerMask groundedMask;
//This is the Player information
GameObject myPlayer;//UFPS Player game object
vp_FirstPersonController myController;//UFPS Player controller
vp_FPInput myInput;//UFPS Player Input
//State of this vehicle, if we are in the vehicle or not
bool InThisVehicle=false;
void Start ()
{
//Set references to everything
thisVehicle = this.GetComponent<Transform>();
myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
//turn off Vehicle scripts and camera on start
myHoverAudio.enabled=false;
myHoverMotor.enabled=false;
myCamera.SetActive(false);
if(myCanvas)
myCanvas.SetActive(false);
}
//Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
void FixedUpdate ()
{
if(InThisVehicle)
{
//Change the Input to whatever you want to use
if(Input.GetKeyDown(KeyCode.Z))
ExitVehicle();
}
}
public void ExitVehicle()
{
myPlayer.SetActive(true);//turn on the player
myController.SetPosition(playerExitPosition.transform.position);//move the player
myInput.enabled=true;//enable the player's input
myHoverMotor.enabled=false;//turn off vehicle stuff
myHoverAudio.enabled=false;//turn off vehicle stuff
myCamera.SetActive(false);//turn off vehicle stuff
InThisVehicle=false;//We aren't in the vehicle anymore
if(myCanvas)
myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float walkSpeed = 8;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
}
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
//if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
//grounded = true;
//}
//else {
// grounded = false;
//}
//}
void FixedUpdate() {
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
}
public void EnterVehicle()
{
myPlayer.SetActive(false);//turn off the player
myHoverMotor.enabled=true;//turn on vehicle stuff
myHoverAudio.enabled=true;//turn on vehicle stuff
myCamera.SetActive(true);//turn on vehicle stuff
InThisVehicle=true;//We are in the vehicle now
if(myCanvas)
myCanvas.
SetActive(true);RequireComponent (typeof (GravityBody))]
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody hoverRigidbody;
}
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
hoverRigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
//Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
//Vector3 targetMoveAmount = moveDir * hoverSpeed;
//moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
grounded = true;
}
else {
grounded = false;
}
}
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);
}
}
}
}
//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)
{
//if a player enters trigger and the player isn't already in the vehicle
//just in case there are multiple players
if(other.gameObject.tag=="Player"&&InThisVehicle==false)
{
myPlayer=other.gameObject;//Get the player
myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
EnterVehicle();//Run the EnterVehicle function
}
}
“//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.?
Unfortunately getting another “a namespace does not directly contain members such as fields or methods unity” around this code
//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);}
//if a player enters trigger and the player isn't already in the vehicle
//just in case there are multiple players
if (other.gameObject.tag=="Player"&&InThisVehicle==false)
myPlayer=other.gameObject;//Get the player
FirstPersonController=myPlayer.GetComponent<FirstPersonController>();//Get the controller
myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
EnterVehicle();//Run the EnterVehicle function
[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.
using System.Collections;
public class red_InteractVehicle : MonoBehaviour {
//This Vehicle Information
Transform thisVehicle;
HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
GameObject myCamera ; //Camera for this vehicle
GameObject playerExitPosition; //Position to put the player on exiting vehicle
public GameObject myCanvas;//Basic control information
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float hoverSpeed = 20;
public float jumpForce = 1000f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
//public void EnterVehivle;
//public void exitvehicle;
public LayerMask groundedMask;
//This is the Player information
GameObject myPlayer;//UFPS Player game object
vp_FirstPersonController myController;//UFPS Player controller
vp_FPInput myInput;//UFPS Player Input
//State of this vehicle, if we are in the vehicle or not
bool InThisVehicle=false;
void Start ()
{
//Set references to everything
thisVehicle = this.GetComponent<Transform>();
myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
//turn off Vehicle scripts and camera on start
myHoverAudio.enabled=false;
myHoverMotor.enabled=false;
myCamera.SetActive(false);
if(myCanvas)
myCanvas.SetActive(false);
}
//Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
void FixedUpdate ()
{
if(InThisVehicle)
{
//Change the Input to whatever you want to use
if(Input.GetKeyDown(KeyCode.Z))
ExitVehicle();
}
}
public void ExitVehicle()
{
myPlayer.SetActive(true);//turn on the player
myController.SetPosition(playerExitPosition.transform.position);//move the player
myInput.enabled=true;//enable the player's input
myHoverMotor.enabled=false;//turn off vehicle stuff
myHoverAudio.enabled=false;//turn off vehicle stuff
myCamera.SetActive(false);//turn off vehicle stuff
InThisVehicle=false;//We aren't in the vehicle anymore
if(myCanvas)
myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float walkSpeed = 8;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
}
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
//if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
//grounded = true;
//}
//else {
// grounded = false;
//}
//}
void FixedUpdate() {
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
}
public void EnterVehicle()
{
myPlayer.SetActive(false);//turn off the player
myHoverMotor.enabled=true;//turn on vehicle stuff
myHoverAudio.enabled=true;//turn on vehicle stuff
myCamera.SetActive(true);//turn on vehicle stuff
InThisVehicle=true;//We are in the vehicle now
if(myCanvas)
myCanvas.
SetActive(true);RequireComponent (typeof (GravityBody))]
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody hoverRigidbody;
}
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
hoverRigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
//Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
//Vector3 targetMoveAmount = moveDir * hoverSpeed;
//moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
grounded = true;
}
else {
grounded = false;
}
}
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);
}
}
}
}
//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)
{
//if a player enters trigger and the player isn't already in the vehicle
//just in case there are multiple players
if(other.gameObject.tag=="Player"&&InThisVehicle==false)
{
myPlayer=other.gameObject;//Get the player
myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
EnterVehicle();//Run the EnterVehicle function
}
}
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.
Remove the last " } " after the last FixedUpdate()
These pages may be a help to you, if you have trouble balancing braces/brackets etc.
Just paste your code and take a look.
using System.Collections;
public class red_InteractVehicle : MonoBehaviour {
//This Vehicle Information
Transform thisVehicle;
HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
GameObject myCamera ; //Camera for this vehicle
GameObject playerExitPosition; //Position to put the player on exiting vehicle
public GameObject myCanvas;//Basic control information
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float hoverSpeed = 20;
public float jumpForce = 1000f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
//public void EnterVehivle;
//public void exitvehicle;
public LayerMask groundedMask;
//This is the Player information
GameObject myPlayer;//UFPS Player game object
vp_FirstPersonController myController;//UFPS Player controller
vp_FPInput myInput;//UFPS Player Input
//State of this vehicle, if we are in the vehicle or not
bool InThisVehicle=false;
void Start ()
{
//Set references to everything
thisVehicle = this.GetComponent<Transform>();
myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
//turn off Vehicle scripts and camera on start
myHoverAudio.enabled=false;
myHoverMotor.enabled=false;
myCamera.SetActive(false);
if(myCanvas)
myCanvas.SetActive(false);
}
//Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
void FixedUpdate ()
{
if(InThisVehicle)
{
//Change the Input to whatever you want to use
if(Input.GetKeyDown(KeyCode.Z))
ExitVehicle();
}
}
public void ExitVehicle()
{
myPlayer.SetActive(true);//turn on the player
myController.SetPosition(playerExitPosition.transform.position);//move the player
myInput.enabled=true;//enable the player's input
myHoverMotor.enabled=false;//turn off vehicle stuff
myHoverAudio.enabled=false;//turn off vehicle stuff
myCamera.SetActive(false);//turn off vehicle stuff
InThisVehicle=false;//We aren't in the vehicle anymore
if(myCanvas)
myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float walkSpeed = 8;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
}
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition rigidbody;position localMove;
}
}
public void; EnterVehicle(){
{
myPlayer.SetActive(false);//turn off the player
myHoverMotor.enabled=true;//turn on vehicle stuff
myHoverAudio.enabled=true;//turn on vehicle stuff
myCamera.SetActive(true);//turn on vehicle stuff
InThisVehicle=true;//We are in the vehicle now
if(myCanvas)
myCanvas.
SetActive(true);RequireComponent (typeof (GravityBody))]
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody hoverRigidbody;
}
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
hoverRigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * hoverSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
//if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
// grounded = true;
//}
//else {
// grounded = false;
//}
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);
}
}
//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)
{
//if a player enters trigger and the player isn't already in the vehicle
//just in case there are multiple players
if(other.gameObject.tag=="Player"&&InThisVehicle==false)
{
myPlayer=other.gameObject;//Get the player
myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
EnterVehicle();//Run the EnterVehicle function
}
}
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
using System.Collections;
public class red_InteractVehicle : MonoBehaviour {
//This Vehicle Information
Transform thisVehicle;
HoverMotor myHoverMotor; //Motor Script, this will be whatever your vehicle uses
HoverAudio myHoverAudio; //Motor Audio Script, this will be whatever your vehicle uses
GameObject myCamera ; //Camera for this vehicle
GameObject playerExitPosition; //Position to put the player on exiting vehicle
public GameObject myCanvas;//Basic control information
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float hoverSpeed = 20;
public float jumpForce = 1000f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
//public void EnterVehivle;
//public void exitvehicle;
public LayerMask groundedMask;
//This is the Player information
GameObject myPlayer;//UFPS Player game object
vp_FirstPersonController myController;//UFPS Player controller
vp_FPInput myInput;//UFPS Player Input
//State of this vehicle, if we are in the vehicle or not
bool InThisVehicle=false;
void Start ()
{
//Set references to everything
thisVehicle = this.GetComponent<Transform>();
myHoverMotor = thisVehicle.GetComponent<HoverMotor>();
myHoverAudio = thisVehicle.GetComponent<HoverAudio>();
myCamera = thisVehicle.GetComponentInChildren<Camera>().gameObject;
playerExitPosition = thisVehicle.FindChild("PlayerExitPosition").gameObject;
//turn off Vehicle scripts and camera on start
myHoverAudio.enabled=false;
myHoverMotor.enabled=false;
myCamera.SetActive(false);
if(myCanvas)
myCanvas.SetActive(false);
}
//Exit vehicle keyboard input. In fixed update so it doesn't move at computer speed
void FixedUpdate ()
{
if(InThisVehicle)
{
//Change the Input to whatever you want to use
if(Input.GetKeyDown(KeyCode.Z))
ExitVehicle();
}
}
public void ExitVehicle()
{
myPlayer.SetActive(true);//turn on the player
myController.SetPosition(playerExitPosition.transform.position);//move the player
myInput.enabled=true;//enable the player's input
myHoverMotor.enabled=false;//turn off vehicle stuff
myHoverAudio.enabled=false;//turn off vehicle stuff
myCamera.SetActive(false);//turn off vehicle stuff
InThisVehicle=false;//We aren't in the vehicle anymore
if(myCanvas)
myCanvas.SetActive(false);[RequireComponent (typeof (GravityBody))}
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float walkSpeed = 8;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody rigidbody;
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
rigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
}
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition rigidbody;position localMove;
}
}
public void; EnterVehicle()
{
myPlayer.SetActive(false);//turn off the player
myHoverMotor.enabled=true;//turn on vehicle stuff
myHoverAudio.enabled=true;//turn on vehicle stuff
myCamera.SetActive(true);//turn on vehicle stuff
InThisVehicle=true;//We are in the vehicle now
if(myCanvas)
myCanvas.
SetActive(true);RequireComponent (typeof (GravityBody))]
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
Rigidbody hoverRigidbody;
}
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
hoverRigidbody = GetComponent<Rigidbody> ();
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-20,20);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
//vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
//Vector3 targetMoveAmount = moveDir * hoverSpeed;
//moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Grounded check
//Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
//if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
// grounded = true;
//}
//else {
// grounded = false;
//}
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);
}
//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)
{
//if a player enters trigger and the player isn't already in the vehicle
//just in case there are multiple players
if(other.gameObject.tag=="Player"&&InThisVehicle==false)
{
myPlayer=other.gameObject;//Get the player
myController=myPlayer.GetComponent<vp_FPController>();//Get the controller
myInput=myPlayer.GetComponent<vp_FPInput>();//Get the input
EnterVehicle();//Run the EnterVehicle function
}
}