Push and Hold Object with Tag

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. 3 box that have been tag with the name “boxes”

heres a screen shot:

THE PROBLEM

  1. 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.
  2. 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.

458280--16031--$Q1.jpg

If I’m not wrong, I recall reading somewhere on the forums that ‘FindWithTag’ only finds the first object with that tag (which is logic, how would it know which of the objects sharing the tag was the one you wanted), in this case it seems that object is your box3.
Findwithtag is only useful if your object tag is unique, ie. there’s only one object with that tag.

On your second code snippet, try changing this:

		if(hit.collider.gameObject.tag == "kotak")
		{
			//To find the object with tag
			object1 = GameObject.FindWithTag ("kotak");

to this:

		if(hit.collider.gameObject.tag == "kotak")
		{
			object1 = hit.collider.gameObject;

I have no idea if that will work…but I guess it will, seems to be working in my little test project.

Thanks, I’ve tried your code but unfortunately it does not work. It puzzle me because there is no error but nothing is working with the raycast script.

Bump

bump

bump, anyone??

Does it have anything to do with push layers?

what do you means “with push layers object”, sorry.

Bump

bump, anyone? help me…

@hizral84
In no shape, way, or form am I an expert, but maybe fresh eyes can help identify your problem(s).

The first thing I noticed was Object1 was identified and assigned in Awake so the value has been set right off. I skimmed through the first script and Object1 was not redefined through out the entire script. Maybe try something similar to your second script like attach a raycast on the player object and use that to detect the collision.

function Update()
{
  var hit : RaycastHit;

  if(Physics.Raycast(transform.position, transform.forward, hit, rayCast))
  {
    if(hit.collider.gameObject.tag == "boxes")
    {
      object1 = hit.gameObject;
    }
  }
}

If I understand it right, that would assign object1 on the fly and may in turn allow you to get you to move a specific box.

Hope that helps.

-S

Thank you all for the help, finally got it working properly.

this what I did:

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 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;
				
				// After detaching from character the selection became empty
				object1 = null;
			}
			
		}
	}
}