- Home /
Vehicle System Help
I have this idea for a system that would allow someone to get into and out of a vehicle. The vehicle would have several gameobjects childed to it, one at the location of the driver's seat with the "Driver" tag, and one at each passenger seat with the "Passenger" tag. The vehicle would detect if you were a certain distance away from it, and if you were, and you pressed E, then it would detect the closest gameobject with the "Driver" or "Passenger" tags, and snap your position to it. If you were in the driver's seat, then it would make the vehicle a child of you, and modify the movement script, shown below, to increase your speed and decrease your jump height to 0. If you were in the passenger's seat, then it would make you a child of the vehicle. It would revert the parenting/childing for you if you jumped out.
I have 2 questions, 1: Is it feasible? 2: If it is, then how would I script it?
public var jumpSpeed:float = 18.0;
public var gravity:float = 32.0;
public var runSpeed:float = 15.0;
public var walkSpeed:float = 45.0;
public var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
static var dead : boolean = false;
function Update ()
{
if(dead == false) {
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
//if(Input.GetButton("Jump"))
if (Input.GetKeyDown(KeyCode.Space))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
// Toggle walking/running with the T key
//if(Input.GetKeyDown("t"))
//isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
//Screen.lockCursor = true;
Screen.showCursor = false;
//var mouse1 = Input.mousePosition.y;
//var mouse2 = Input.mousePosition.x;
}
//Vector3 mousePos = Input.mousePosition;
else {
//Screen.lockCursor = false;
Screen.showCursor = false;
//Input.mousePosition.y = mouse1;
//Input.mousePosition.x = mouse2;
//Input.mousePosition = mousePos;
}
}
@script RequireComponent(CharacterController)
Answer by Benifir · Jul 25, 2013 at 08:09 PM
Yes, it is completely feasible. I don't know how to un-parent a game object but all you have to do is get the transform of the player and the transform of the seat and check if the distance between the two is less than the allowed distance (make an int/float variable for it). Then its just a simple if statement. You should find game objects with tage and cache the player transform as well as the seats transfrom. For example:
if((_player.transform.position - _myTransform.position) < maxDistance)
DoSomethingHere();
This should give you a start to go on (:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[Help] FormatException: Input string was not in the correct format System.Int32.Parse 0 Answers
Accessing Functions from other Scripts 1 Answer
An object reference is required for the non-static field, method, or property 1 Answer
Boat movement, driving and animation 1 Answer