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

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

478818--16822--$PushChair1.jpg

Bump. Anyone??

Points for a detailed and specific post :slight_smile:

I tried the webplayer, but the controls don’t seem to be working here (IE). Q and W are rotating the camera (rather than Q and E), but I can’t move the player (other than jumping).

Does the webplayer work correctly for you?

mmm i see a bug when i rotate the camera. but the problem is that the character only can push forward if is enter in collision.

i want to mean if u press z all time, u can push well…

maybe u can do it with the OnCollisionEnter… I don’t know

Thanks and sorry about that…I accidently wrote WASD as the character controller actually its the ARROW KEYS my bad…and yes your right Q and W is the rotate keys…maybe you can try it again…thank…and sorry for the inconvenient…

What type of bug did you get when rotate the camera?

Im using a Raycast script…so the character have to move closer to the chair to make it work…if the character move to close to the chair it will make the character controller stuck and can move only one direction…and if you position the character a bit far from the chair its work fine…

how do I set the character to stop from getting to close with the chair let’s say if the distance between the chair and the character is 5 unit…the character cannot come closer anymore with the chair and I would like to make my character can only push the faces of the chair only not the corner as the picture shown…

At first I wanted to used trigger, I want to set 4 trigger on all the face of the chair as the picture shown…but as from what I learn we can only put one box collider at a time, so need to put the Idea away.

And for some reason when you pushing the chair and move to the wall the chair will go through the wall…what is that…how do I correct that error.

i want to mean that if u rotate the camera, the direction of pushing is not aligned with the direction of the character

Yes, thats the other problem that I have and need to be fix…Im stil trying to fix that…

THE CODE:

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)

Bump.

Bump.

I manage to fix the problem for 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:
http://www.lcgdi.com/Upload/LCGDI%20TEST%20AREA/NomuraWeb/WebPlayer.html

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)

anyone??

Sorry you’re languishing with no help :slight_smile: The thread’s a little dense at this point now - any chance you could sum up and/or restate the question, just as a reminder as to what problem you’re trying to solve? (I know we could re-read the thread, but a re-statement probably wouldn’t hurt.)

Thanks for the advise I will do that.

THE 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 NEW PROBLEM

As Jesse Anders said earlier, What I’ve done so far is I manage fix the problem for 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??

Second, still need to find a way to make a specific area for the chair so that the character can push it, for example the character can only push the face of the chair only not the corner.

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

last but not least, have a GUI problem. How do you make a GUI stay on one position. For example what I did is, my character step on a trigger and a GUI pop up in front of the wall and tell what to do. If I rotate my camera the GUI will not stay at the same position because it’s follow the camera movement. How do I solve this problem.

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;
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)

The new Controller set while pushing the chair

Well, I have to admit, I’m having a hard time understanding the problem. Your post is detailed and clear, but there’s a lot it in (for example, the GUI question at least could probably go in its own thread).

If you don’t get any answers from this thread, what I’d suggest is isolating one of the problems and presenting that problem only, with a webplayer and instructions for how to reproduce the problem.

Anyway, sorry I can’t be more helpful. (I did read over the post a couple of times, but for some reason I’m just not quite tracking on it.)

For the GUI question I did posted it inside GUI thread, just put it here to summarize what i’ve manage to do and what new problem that i’ve bump into. I also ask a few of my friend to look about my problem, putting it inside here just one of my option on solving the problem.

I’ll try and isolate all my problem one by one using different thread after this, thanks.

don’t use a raycast to detect the chair, set up four box colliders as children of the chair. Place the four box colliders on the four sides of the chair and do function OnTriggerStay() to check if the player is within range to move the chair.

Set the colliders to IGNORE RAYCAST or create a new layer and add the layer number to the physics.raycast layermask.

Also don’t waist time with a find object by tag. Just use transform.GetComponent

transform. = the gameObject the script is attached too.

then do something like this.

//this should be equal to the width and length of the object your trying to push 
//so the raycast can reach the otherside
var rayLength : float = 1

//if you character doesn't bounce back enough set this value higher
var bounceStrength : float = 5.0;

// create var to hold the character controller
private var controller : CharacterController;

function Awake(){
// get the character controller on the current character and set to controller var
controller = GetComponent(CharacterController);

//add 0.3 to give a little prediction space to the raycast.
rayLength += 0.3
}

function OnTriggerStay (hit : Collider){
	if (hit.collider.tag == "Drag"){
		Debug.Log("Contact With Chair!");
		if(Input.GetButton ("Button Z"))
			{
				//To disable the marioController and marioAnimation
				transform.GetComponent(SuperMarioController).enabled = false;
				transform.GetComponent(SuperMarioAnimation).enabled = false;
		
				//To enable the second character controller after holding object
				transform.GetComponent(ScriptControllerHoldObject).enabled = true;
				
				//Attach the object to character
				hit.transform.root.parent = transform.transform;
				
				//Test for collision
				//set ray length by using the radius + rayLength + 0.3 to negate dead space inside the controller
				if (Physics.Raycast (transform.position, transform.forward, hit, (controller.radius + hit.collider.bounds)){
					//this will force your character away from the wall
					transform.Translate(0,0, Time.deltaTime * -bounceStrength);
				}
			}
			else
			{
				//To enable the marioController and marioAnimation
				transform.GetComponent(SuperMarioController).enabled = true;
				transform.GetComponent(SuperMarioAnimation).enabled = true;
		
				//To disable the second character controller after holding object
				transform.GetComponent(ScriptControllerHoldObject).enabled = false;
		
				//Detach the object to character
				object1.transform.root.parent = null;
				
				// After detaching from character the selection became empty
				object1 = null;
			}
			
		}
}

Did not test this, but the theory should work just fine. I’ve tested something identical before.

Hello thanks for the reply,

I did what you said, I setup 4 collider on each side of the chair and make them as trigger. I used the code that you give, have a bit of a problem,

//Test for collision
//set ray length by using the radius + rayLength + 0.3 to negate dead space inside the controller
if(Physics.Raycast(transform.position, transform.forward, hit, (controller.radius + hit.collider.bounds)))
{
    //this will force your character away from the wall
    transform.Translate(0, 0, Time.deltaTime * -bounceStrength);
}

Got this error:
Assets/_NOMURA/_Scripts/_Player/NewBehaviourScript.js(40,43): BCE0023: No appropriate version of ‘UnityEngine.Physics.Raycast’ for the argument list ‘(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Collider, System.Object)’ was found.

and if I comment out the code above it still work but bump into another problem. After pressing the button and pushing the object, unpressing the button doesn’t detach the chair from the character.

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

One more thing, I know this would sound senseless, where do you put the code actually. I put it inside my character. From my understanding if trigger we put it inside the trigger inspector.

It looks like there are a couple problems with that code. ‘hit’ should be of type RaycastHit (not Collider). Also, Collider.bounds is a bounding box, so it can’t be added to a scalar value.

Ahhh yes, it should be.

if (hit.gameObject.tag == "Drag")

Still having trouble understanding this, could you explain a bit detail. Thanks you.

Still waiting for the TheLlama to reply back.

[quote]
Still having trouble understanding this, could you explain a bit detail.
[/quote]I’m guessing the The Llama meant for the sum of the effective radii of the player and the target object to be computed (or something like that), but I’m not sure.

Anyway, you can’t add a scalar and a bounding box because programming-wise the operation isn’t supported, and semantically the operation has no meaning. (If that still doesn’t make sense, just ask yourself, what would it mean to add a scalar value and a bounding box? What would the result be?)