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
- 3 box that have been tag with the name “boxes”
heres a screen shot:
THE PROBLEM
- I manage to make the character hold and push the object by attaching the box to the character and change the controller type, but with all the three box been tag “boxes” I can only push 1 box that is box number 3 the other 2 box I cannot push it.
- I write my script using “OnControllerColliderHit”, this make the character can pick up the box at any distance. I’ve try change the script using raycast but have problem, the script does not work but have no error at all.
THE SCRIPT
var object1 : GameObject;
var object2 : GameObject;
function Awake()
{
//To find the object with tag
object1 = GameObject.FindWithTag ("kotak");
object2 = GameObject.FindWithTag ("Player");
}
function OnControllerColliderHit(hit : ControllerColliderHit)
{
//Press the button and hold to hold object
if(Input.GetButton ("Button A"))
{
//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;
}
}
Using “OnControllerColliderHit”, when I play start it automaticly detect box3 so I can only push box3 not the others
var object1 : GameObject;
var object2 : GameObject;
var rayCast : float = 5.0;
function Awake()
{
//To find the object with tag
object2 = GameObject.FindWithTag ("Player");
}
function update()
{
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, rayCast))
{
if(hit.collider.gameObject.tag == "kotak")
{
//To find the object with tag
object1 = GameObject.FindWithTag ("kotak");
//Press the button and hold to hold object
if(Input.GetButton ("Button A"))
{
//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;
}
}
}
}
The RayCast script that I did.
