- Home /
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[i].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?"
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);
}
Is the "Player" have is skeleton as a child or any children? That would explain some of your result. Because these gameobject are picked by your sphere.
If it's possible, I would suggest that answer by @numberkruncher
foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) {
var interactable = collider.GetComponent<InteractableObject>();
if (interactable == null)
continue;
interactable.Interact(m_forcePoints);
}
Adding an InteractableObject script is probably the best way to handle this and allow easier implementation of futur functionality
Answer by numberkruncher · Nov 27, 2013 at 12:27 AM
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[i].transform))
continue;
Other than that, I am really unsure as to how I can help...
Sorry my internet was down! Excellent answer thank you!!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Using Conditional Compilers? 1 Answer
Distribute terrain in zones 3 Answers
Unity and Leap Motion: Help with Jerkiness of input! 1 Answer
iTween Visual Editor Event Call from C# to Javascript 1 Answer