I am trying to program my FPS game. It’s working like this:
I am near car and press E button. My FPS man go drive car now. But i have few problems.
One. How i can make camera follow the car?
Two: I use rigibody physics and one of my try is bad. When i jump my car my car go REALLY fast some way. Bouncing. Code is this:
Code below is not C# but JS-like, now obsolete, Unity Script
var Player : Transform;
var Speed : float;
var CarSpeed: float;
var StopSpeed = 0.0;
var InCarPosition : Transform;
var PlayerIn = false;
var ExitPoint : Transform;
var CanChange = true;
var turnSpeed: float;
function Update () {
transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
Player = GameObject.FindWithTag("Player").transform;
if(PlayerIn == true){
gameObject.GetComponent("First Person Controller").enabled=false;
Player.transform.position.y = InCarPosition.transform.position.y;
Player.transform.position.z = InCarPosition.transform.position.z;
Player.transform.position.x = InCarPosition.transform.position.x;
}else{
}
if(Input.GetKey(KeyCode.W)&& PlayerIn == true){
rigidbody.velocity = transform.forward * Speed *Input.GetAxis("car");
rigidbody.velocity.y = Input.GetAxis("root");
}
if(Vector3.Distance(Player.position,transform.position) < 5){
if(Input.GetKey(KeyCode.E)&& PlayerIn == false){
if(CanChange == true){
CanChange = false;
Change();
Player.transform.position.y = InCarPosition.transform.position.y;
Player.transform.position.x = InCarPosition.transform.position.x;
Player.transform.position.z = InCarPosition.transform.position.z;
PlayerIn = true;
Speed = CarSpeed;
}
}else{
if(Input.GetKey(KeyCode.E)&& PlayerIn == true){
if(CanChange == true){
CanChange = false;
Change();
PlayerIn = false;
Speed = StopSpeed;
}
}
}
}
}
function Change(){
yield WaitForSeconds(1);
CanChange = true;
}
my other try is some “cube” going front of car and we move that cube when we are in car and car follow that. But not working code:
var Player : Transform;
var Speed : float;
var CarSpeed: float;
var StopSpeed = 0.0;
var InCarPosition : Transform;
var PlayerIn = false;
var ExitPoint : Transform;
var CanChange = true;
var turnSpeed: float;
function Update () {
transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
Player = GameObject.FindWithTag("Player").transform;
if(PlayerIn == true){
gameObject.GetComponent("First Person Controller").enabled=false;
Player.transform.position.y = InCarPosition.transform.position.y;
Player.transform.position.z = InCarPosition.transform.position.z;
Player.transform.position.x = InCarPosition.transform.position.x;
}else{
}
if(Input.GetKey(KeyCode.W)&& PlayerIn == true){
rigidbody.velocity = transform.forward * Speed *Input.GetAxis("car");
rigidbody.velocity.y = Input.GetAxis("root");
}
if(Vector3.Distance(Player.position,transform.position) < 5){
if(Input.GetKey(KeyCode.E)&& PlayerIn == false){
if(CanChange == true){
CanChange = false;
Change();
Player.transform.position.y = InCarPosition.transform.position.y;
Player.transform.position.x = InCarPosition.transform.position.x;
Player.transform.position.z = InCarPosition.transform.position.z;
PlayerIn = true;
Speed = CarSpeed;
}
}else{
if(Input.GetKey(KeyCode.E)&& PlayerIn == true){
if(CanChange == true){
CanChange = false;
Change();
PlayerIn = false;
Speed = StopSpeed;
}
}
}
}
}
function Change(){
yield WaitForSeconds(1);
CanChange = true;
}