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
- The character
- 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
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