- Home /
My character controller and vehicle (spaceship) move at same time
Hi,
I have a FPS game, that includes a FPS Ctrl and a Spaceship.
My problem is that the character and the ship move together on the scene, and I need the FPS ctrl to be able to enter the Spaceship so it can be controlled. Also, this script is best put into the FPS ctrl or the Spaceship? I'm using I'm sorry if this is some repost, but I haven't found something similar that helped. Here's the code I have on the Spaceship so far...
I'm very noob with coding yet...
thanks btw.
var camera1 : Camera;
var camera2 : Camera;
var turnspeed = 5.0;
var speed = 5.0;
private var trueSpeed = 0.0;
var strafeSpeed = 5.0;
var InShip : Transform;
var Player : Transform;
var PlayerIn = false;
var ExitPoint = Transform;
var CanChange = true;
function Update () {
var roll = Input.GetAxis("Roll");
var pitch = Input.GetAxis("Pitch");
var yaw = Input.GetAxis("Yaw");
var strafe = Vector3(Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime, Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime, 0);
var power = Input.GetAxis("Power");
Player = GameObject.FindWithTag("Player").transform;
if(PlayerIn == true)
{
Player.transform.position.y = InShip.transform.position.y;
Player.transform.position.z = InShip.transform.position.z;
Player.transform.position.x = InShip.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");
}
//Truespeed controls
if (trueSpeed < 3000 && trueSpeed > -3000)
{
trueSpeed += power;
}
if (trueSpeed > 3000)
{
trueSpeed = 0;
}
if (trueSpeed < -3000)
{
trueSpeed = 0;
}
if (Input.GetKey("c"))
{
trueSpeed = 0;
currentVelocity = rigidbody.velocity;
oppositeForce = -currentVelocity;
rigidbody.AddRelativeForce(oppositeForce.x, oppositeForce.y, oppositeForce.z);
print(trueSpeed);
}
rigidbody.AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, roll*turnspeed*Time.deltaTime);
rigidbody.AddRelativeForce(0,0,trueSpeed*speed*Time.deltaTime);
rigidbody.AddRelativeForce(strafe);
}
function OnTriggerEnter(other : Collider)
{
if(GameObject.FindWithTag("Player")) && (GameObject.FindWithTag ==("Spaceship")
{
camera1.enabled = false;
camera2.enabled = true;
}
}
It may be better to have the 2 control types separated, and decide which to use by a simple boolean switch :
var isFPS : boolean = true;
when you enter/exit the spaceship, toggle the boolean true/false. YOu probably want to switch cameras here too.
switch( isFPS )
{
case true :
// do FPS control stuff here
break;
case false:
// do Spaceship control stuff here
break;
}
when you have separated the controls, it is easier to manage what happens when walking around and what happens when piloting.
Answer by daverocks2 · Dec 11, 2012 at 07:28 AM
You could try
var isPlayerVisible : boolean;
If (input.getAxis("Horizontal") && isPlayerVisible)
Just replace the PlayerIn var with isPlayerVisible