- Home /
Hover Car Controller?
Ok I got my hover script working and my hover car hovers although I have yet to learn how to make a controller for the car Simply
Move Forward Relatitve to the camera attached to the car. Rotate left or right upon turning. and move backwards relative to the attached camera
I have tryed attaching the first / third person controlser to it and disableing / enabling them when you get in /out o the car i have attempted to use
using UnityEngine;
using System.Collections;
public class ControllableSphereComponent : MonoBehaviour
{
public float speed= 30;
public float Force;
void FixedUpdate()
{
if (Input.GetKey("down"))
{
this.rigidbody.AddForce(-Time.deltaTime * Force, 0, 0, ForceMode.Acceleration);
}
if (Input.GetKey("left"))
{
transform.Rotate (Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey("right"))
{
transform.Rotate (-Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey("up"))
{
this.rigidbody.AddForce(Time.deltaTime * Force, 0, 0, ForceMode.Acceleration);
Debug.Log("pressRegisterd");
}
}
}
The Problem is if I turn and then go forward it dosent move forward is moves what ever way was forward before I turned.
And I haven't successfully got the Vector3 to move the car forward or backwards.
If you attach a script Please either comment it out or explain how the script works i am here to learn not to get free scripts
Also My Script to enter and Exit the car is:
static var selectedId : int;
static var speed : int = 5;
var lookSpeed = 15.0;
var moveSpeed = 15.0;
var rotationX = 0.0;
var rotationY = 0.0;
var movespeed=50;
var walking : boolean=true;
var minimumEnterRange : float = 10.0;
var currentVehicle : GameObject;
public var rearCamera : Camera ;
public var driverCamera : Camera ;
function Start(){
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
}
function Update(){
if (Input.GetKey(KeyCode.I))
{
rearCamera.camera.enabled = false;
driverCamera.camera.enabled = true;
}
else if (Input.GetKey(KeyCode.O))
{
driverCamera.camera.enabled = false;
rearCamera.camera.enabled = true;
}
if(Input.GetKeyDown(KeyCode.H)){
if(walking){
// find all the vehicles in the scene
var vehicles : GameObject[] = GameObject.FindGameObjectsWithTag ("Vehical");
// find the closest vehicle we are looking at
for(var vehicle : GameObject in vehicles){
if(Vector3.Distance(vehicle.transform.position, transform.position) < minimumEnterRange){
var vehicleInCameraSpace = Camera.main.transform.InverseTransformPoint(vehicle.transform.position).normalized;
if(Vector3.Dot(Camera.main.transform.forward, vehicleInCameraSpace)>.3){
currentVehicle = vehicle;
break;
}
}
}
// if we can find a vehicle close enough then lets get in it.
if(currentVehicle){
walking = false;
rearCamera.camera.enabled = false;
driverCamera.camera.enabled = true;
Camera.main.GetComponent(CharacterController).enabled=false;
Camera.main.GetComponent(CharacterMotor).enabled=false;
Camera.main.GetComponent(FPSInputController).enabled=false;
Camera.main.GetComponent(MouseLook).enabled=false;
Camera.main.GetComponent(SmoothFollow).enabled=false;
Camera.main.GetComponent(SmoothFollow).target=transform;
Camera.main.GetComponent(CharacterMotor).enabled=false;
currentVehicle.GetComponent(CharacterController).enabled=true;
currentVehicle.GetComponent(CharacterMotor).enabled=true;
currentVehicle.GetComponent(FPSInputController).enabled=true;
rearCamera.camera.enabled = false;
driverCamera.camera.enabled = true;
}
} else {
// if we are currently driving then lets get out of the vehicle.
walking = true;
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
Camera.main.GetComponent(CharacterController).enabled=true;
Camera.main.GetComponent(CharacterMotor).enabled=true;
Camera.main.GetComponent(FPSInputController).enabled=true;
Camera.main.GetComponent(MouseLook).enabled=true;
Camera.main.GetComponent(SmoothFollow).enabled=false;
Camera.main.GetComponent(CharacterController).CanControl.enabled=true;
currentVehicle = null;
//car stuff disable
currentVehicle.GetComponent(CharacterController).enabled=false;
currentVehicle.GetComponent(CharacterMotor).enabled=false;
currentVehicle.GetComponent(FPSInputController).enabled=false;
rearCamera.camera.enabled = true;
driverCamera.camera.enabled = false;
}
}
}
P.S: I know theres other Questions like this but none of them helped me.