- Home /
make addforce ignore player controller?
Hello Everyone, I am currently working on a 3d FPS game that enables the player to enter and pilot a underwater submarine pod. The pod uses the addRelativeForce on it's rigidbody since it slowly accelerates and drags as if underwater.
The problem is, my character's player controller is being seen as an outside force, even if I add the character (child) to the ship (parent) in the hierarchy.
If the ship does any sort of movement such as turning or ascending, the player is able to push against the ship and make it slow down or suddenly fly off into a random angle. The player has a rigidbody as well but with 0 mass,drag, and angular drag.
Does anyone know if there is a way to make the character controller unable to affect the addforce? I know I can use "position.transform.y" but then the ship doesn't react to outside colliders (such as rocks)..
Thank you for taking the time to read this, and I hope someone can point me in the right direction.
I have attached my script for the addforce in case it is needed.
#pragma strict
var theplayer: Transform;
var theplayercamera: GameObject;
var theplayermovement : FPSWalkerEnhanced;
private var drawGUI = false;
var thechair : GameObject;
var theShip : GameObject;
var respawnTransform : Transform;
var respawnTransform2 : Transform;
var seatOccupied : boolean = false;
var chairCamera : GameObject;
var driveCommands : startingshipdrivingscript;
function Update ()
{
if (drawGUI == true && Input.GetKeyDown(KeyCode.E) && seatOccupied == false)
{
enterchair();
}
else if ((Input.GetKeyDown(KeyCode.E) || Input.GetButton("Jump"))&& seatOccupied ==true)
{
exitchair();
}
if (seatOccupied == true)
{
drawGUI = false;
driving();
}
}
function OnTriggerEnter (theCollider :Collider)
{
if (theCollider.tag == "Player" && seatOccupied == false)
{
drawGUI = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
drawGUI = false;
}
}
function OnGUI ()
{
if (drawGUI == true)
{
GUI.Box (Rect (Screen.width*0.5, 400, 300, 26), "Press E to Pilot");
}
}
function enterchair ()
{
seatOccupied =true;
theplayer = GameObject.FindGameObjectWithTag("Player").transform;
theplayer.GetComponent(MouseLook).enabled = false;
theplayermovement = theplayer.GetComponent(FPSWalkerEnhanced);
theplayermovement.walkSpeed = 0;
theplayermovement.runSpeed = 0;
theplayercamera = GameObject.Find("Main Camera");
theplayercamera.SetActive(false);
chairCamera.SetActive (true);
theplayer.parent = thechair.transform;
theplayer.transform.position = respawnTransform.position;
theplayer.transform.rotation = respawnTransform.rotation;
}
function exitchair ()
{
seatOccupied =false;
theplayer = GameObject.FindGameObjectWithTag("Player").transform;
theplayer.GetComponent(MouseLook).enabled = true;
theplayermovement.walkSpeed = 6.0;
theplayermovement.runSpeed = 11.0;
theplayercamera.SetActive(true);
chairCamera.SetActive (false);
theplayer.parent = theShip.transform;
theplayer.transform.position = respawnTransform2.position;
theplayer.transform.rotation = respawnTransform2.rotation;
}
function driving()
{
if (Input.GetKeyDown(KeyCode.W))
{
driveCommands.gear +=1;
}
if (Input.GetKeyDown(KeyCode.S))
{
driveCommands.gear -=1;
}
if (Input.GetKeyDown(KeyCode.D))
{
driveCommands.turngear +=1;
}
if (Input.GetKeyDown(KeyCode.A))
{
driveCommands.turngear -=1;
}
if (Input.GetKeyDown(KeyCode.R))
{
driveCommands.raiseup +=1;
}
if (Input.GetKeyDown(KeyCode.F))
{
driveCommands.raiseup -=1;
}
}
As I know character controller can not be used with rigidbody and it's main idea is to give an easy way to make games to those guys who lack knowledge of physics or just are too lazy to make things right way. :)
So to fix your problem, as I understand, is to disable character controller when you are controlling your sub and enable it when you leave the pilot's seat.
my apologies, I meant that it causes that issue when he walks around in the sub. You can exit the pilot seat and walk around to repair parts of the submarine that get damaged or to leave the sub.
You know that character controller ignores physics and that is how it is supposed to work? Just make a simple character with rigidbody and collider, it's not that hard or will take long time, but solves your problem.
I got ahold of a character controller script, but now the character doesn't move when the ship moves or rotates. He just sort of slides around in place until he hits a wall.
I increased the floor material friction, so he doesn't seem to slide around, but still doesn't rotate when the ship does...
Answer by Nanako · Nov 24, 2014 at 01:45 PM
If it's not what you're already doing; i'd solve this by parenting the player to the sub.
And also by increasing the mass of the sub significantly
I don't think objectd can exert forces on their parent like that, but i could be wrong. Even if so, there's other things you could do to compensate. IMO passengers and drivers should always be parented to a vehicle they're in, so that it becomes their frame of reference.