How to exclude Player from Picking himself up! C#

Hello,

I am trying to pick up objects only in my viewfrustrum, not occluded from my view and exclude the player, WHICH IS in my view ugh…
Because its a third person camera. So I am thinking that is the issue. I can only pick up objects when i comment out the line “if (obj.name == “Player”) return false;” BUT THE PLAYER IS PICKED UP TOO! I DONT want that to happen…

“ObjectInteractable” simply excludes objects that do not meet the criteria for being able to be moved by the controller… Thanks!

// returns condition for if force should affect this object.
	bool ObjectInteractable(GameObject obj) {
		if (obj.rigidbody == null) return false;
		if (obj.name == "Player") return false;
		
		// ignore objects not in the view frustum.
		if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(Camera.main), obj.collider.bounds) == false) {
			return false;
		}
		
		// ignore objects that are occluded from our view.
		RaycastHit hit;
		if(Physics.Linecast(transform.position, obj.transform.position, out hit)) {
			if (hit.transform != obj.transform) return false;	
		}
		
		return true;
	}
	
	void Levetate() {
		if (Time.time - m_lastPushTime < 1.0f) return;
		StageOneLevetate();
	}
	
	void StageOneLevetate() {
		Collider [] m_pullObjects = Physics.OverlapSphere(transform.position, 10.0f);
		for(int i = 0; i < m_pullObjects.Length; ++i) {
			GameObject gObject = m_pullObjects*.transform.gameObject;*
  •  	if (ObjectInteractable(gObject) == false) continue;*
    
  •  	if (gObject.rigidbody.mass < 1.0f) {*
    

gObject.rigidbody.velocity = Vector3.up * Random.Range(0.5f, 0.8f) * (1.0f + (m_forcePoints / 500.0f));

  •  		gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f)));	*
    
  •  	}*
    
  •  	else if (gObject.rigidbody.mass < 5.0f) {*
    

gObject.rigidbody.velocity = Vector3.up * Random.Range(0.3f, 0.4f) * (1.0f + (m_forcePoints / 500.0f));

  •  		gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f)));*
    
  •  	}*
    
  •  }*
    
  • }*
    Thanks for the help!

You are more likely to get help if you give your question an actual question title! Perhaps "How to pick up objects in view frustum?"

Thank you sir, Good point!

@numberkruncher do you have any ideas?

At line #30 in the source of your question (immediately above if (ObjectInteractable(gObject ... please insert the following and let me know the results since this might help to highlight the cause of your problem: Debug.Log(gObject.name); Generally I would have a separate script attached to the interactable objects. For instance: foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) { var interactable = collider.GetComponent<InteractableObject>(); if (interactable == null) continue; interactable.Interact(m_forcePoints); }

@numberkruncher With Line #4 commented out, I get all the bones of my character... With out that Line #4 commented out, i get the same thing BUT my character doesn't move and NO OTHER OBJ move either... All the above is understandable because it hasn't discriminated if the gObject can be used or not... I'm so stumped as to what is wrong Im Not sure that i want it to be separate because im using many objs

1 Answer

1

Scripts for interactable objects

Generally I would have a separate script attached to the interactable objects. For instance:

foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) {
    var interactable = collider.GetComponent<InteractableObject>();
    if (interactable == null)
        continue;

    interactable.Interact(m_forcePoints);
}

Eliminate objects within character hierarchy

Or, to eliminate objects within the hierarchy of your character you could using something like the following:

public bool IsPlayerObject(Transform t) {
    var playerTransform = transform;
    while (t != null) {
        if (t == playerTransform)
            return true;
        t = t.parent;
    }
    return false;
}

And then:

if (IsPlayerObject(m_pullObjects*.transform))*

continue;
Other than that, I am really unsure as to how I can help…

Sorry my internet was down! Excellent answer thank you!!