Driving car FPS
Asked Answered
F

2

0

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;
}
Furst answered 6/6 at 14:9 Comment(0)
M
0

8582-35735859.jpg

Men answered 6/6 at 14:10 Comment(0)
O
0

Solution
Add a empty game object to car name it “lookat” delete your recent car camera.

Follow camera: install cinematic package
Click player game object → add virtual cinematic camera in it instector look at and follow section add “lookat” and done

For 2nd problem:
In canChange is true condition add line
rigidbody.enable=false;
And when player exit section add line
rigidbody.enable=true;

Orosco answered 5/6 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.