HELP...PARENT, CHILD and COLLIDER Problem.

hello again,

SCENARIO:

What im doing is my character can walk in front of an object and hold and push the object with a hold of a button. When the character push the object the controller type will be change i.e from a normal 3rd person controller to grid type contoller.

THE STAGE

  1. The character
  2. 1 chair that have a tag "Drag"

Here's the Web Player to test it out:

http://www.lcgdi.com/Upload/LCGDI%20TEST%20AREA/NomuraWeb/WebPlayer.html

controller:

ARROW KEYS = Character movement

SPACE = Jump

Z = Press Button

Q and W = Rotate Camera 90 degrees

THE PROBLEM alt text

I manage to make the character hold and push the object by attaching the chair to the character and change the controller type. But if the character is at the coner I want the character to snap at the center position of the chair when I pressed the button down.

Second, after I push the chair and move it up a wall, the chair move through the wall, the chair collider appear disable.

Third, when the character controller state change when I start push the chair, and rotate the camera the controller didnt move according relative to the camera.

I think you guys will understand what I'm trying to say when you try the web player above.

THE SCRIPT


var rayCastLength : float = 5.0;
var object1 : GameObject;
var object2 : GameObject;

function Awake()
{
    //To find the object with tag
    object2 = GameObject.FindWithTag ("Player");
    Debug.Log("Yes I Found You Player!");
}

function Update () 
{
    var hit : RaycastHit;

    if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
    {
        if(hit.collider.gameObject.tag == "Drag")
        {
            Debug.Log("Contact With Chair!");

            //To find the object with tag
            object1 = hit.collider.gameObject;
            Debug.Log("Yes I Found You Chair!");

            //Press the button and hold to hold object
            if(Input.GetButton ("Button Z"))
            {
                //To disable the marioController and marioAnimation
                object2.GetComponent(SuperMarioController).enabled = false;
                object2.GetComponent(SuperMarioAnimation).enabled = false;

                //To enable the second character controller after holding object
                object2.GetComponent(ScriptControllerHoldObject).enabled = true;

                //Attach the object to character
                object1.transform.parent = object2.transform;
            }
            else
            {
                //To enable the marioController and marioAnimation
                object2.GetComponent(SuperMarioController).enabled = true;
                object2.GetComponent(SuperMarioAnimation).enabled = true;

                //To disable the second character controller after holding object
                object2.GetComponent(ScriptControllerHoldObject).enabled = false;

                //Detach the object to character
                object1.transform.parent = null;

                // After detaching from character the selection became empty
                object1 = null;
            }

        }
    }
}

To change the controller script for the character


var curSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;

function Update () 
{
    //smoothMovement();

    var controller : CharacterController = GetComponent(CharacterController);

    // Move the character
    var moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
    controller.SimpleMove(moveDirection * curSpeed);
}

@script RequireComponent(CharacterController)

Controller set while pushing the chair

I manage to fix one of the problem,its the camera relative problem..but something else happen. When I try to move my character and then stop my character suddenly slide all the way to the direction that I push the chair.Anyone know what did I do wrong??

you will understand more by playing the NEW WEB PLAYER

and here's the code that I made:


var curSpeed : float = 3.0;
var smoothDirection : float = 3.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update ()
{
    var controller : CharacterController = GetComponent(CharacterController);

    // Calculate the x-axis relative to the camera
    var cam : Transform = Camera.main.transform;
    // Forward vector relative to the camera along the x-z plane
    var forward : Vector3 = cam.TransformDirection (Vector3.forward);

    forward.y = 0;
    forward = forward.normalized;
    var right = Vector3(forward.z, 0, -forward.x);

    var targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;

    if (targetDirection != Vector3.zero)
    {
        moveDirection = Vector3.Lerp(moveDirection, targetDirection, smoothDirection * Time.deltaTime);
    }

    controller.SimpleMove(moveDirection * curSpeed);
}

@script RequireComponent(CharacterController)