I’m making a game where the player has a grappling hook they can use to pull themselves to things and other things to them. Whether the player gets pulled or the object is based on their respective weights. I have it working fine on the enemies, but I’m having an issue with doing the same to interactive environment pieces. So far as I can tell, I have it set up the same way between the enemy portion and the object portion.
I’ve included them both for comparison’s sake.
if(enemyStruck != null){
if(player.GetComponent<CombatScript>().totalWeight < enemyWeight){
Vector3 offset = enemyStruck.transform.position - player.transform.position;
if(offset.magnitude > 0.1f){
offset = offset.normalized;
player.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
}else if (player.GetComponent<CombatScript>().totalWeight > enemyWeight){
Vector3 offset = player.transform.position - enemyStruck.transform.position;
if(offset.magnitude > 0.1f){
offset = offset.normalized;
gameObject.transform.LookAt(player.transform.position);
gameObject.transform.Translate (Vector3.forward * Time.deltaTime*20.0f);
enemyStruck.transform.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
}else if (player.GetComponent<CombatScript>().totalWeight == enemyWeight){
Debug.Log ("Weight equal");
Vector3 offset = player.transform.position - enemyStruck.transform.position;
if(offset.magnitude > 0.1f){
//Debug.Log ("First if");
offset = offset.normalized;
gameObject.transform.LookAt(player.transform.position);
gameObject.transform.Translate (Vector3.forward * Time.deltaTime*20.0f);
enemyStruck.transform.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
offset = enemyStruck.transform.position - player.transform.position;
if(offset.magnitude > 0.1f){
//Debug.Log ("Second if");
offset = offset.normalized;
player.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
}
}
That's the code I have for doing it for the enemy. The following is what I'm doing for the object:
if (objectStruck != null) {
Debug.Log("My weight " + player.GetComponent<CombatScript>().totalWeight);
Debug.Log ("Object weight " + objectStruck.transform.GetComponent<Rigidbody>().mass);
Debug.Log (objectStruck);
if(player.GetComponent<CombatScript>().totalWeight < objectWeight){
Debug.Log ("Object is more");
Vector3 offset = objectStruck.transform.position - player.transform.position;
if(offset.magnitude > 0.1f){
offset = offset.normalized;
player.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
}else if (player.GetComponent<CombatScript>().totalWeight > objectWeight){
Debug.Log ("Object is less");
Vector3 offset = player.transform.position - objectStruck.transform.position;
if(offset.magnitude > 0.1f){
offset = offset.normalized;
gameObject.transform.LookAt(player.transform.position);
gameObject.transform.Translate (Vector3.forward * Time.deltaTime*20.0f);
objectStruck.transform.GetComponent<Rigidbody>().velocity = (offset * 20.0f);
}
}else if (player.GetComponent<CombatScript>().totalWeight == objectWeight){
Debug.Log ("Weight equal");
Vector3 offset = player.transform.position - objectStruck.transform.position;
if(offset.magnitude > 0.1f){
//Debug.Log ("First if");
offset = offset.normalized;
gameObject.transform.LookAt(player.transform.position);
gameObject.transform.Translate (Vector3.forward * Time.deltaTime*20.0f);
objectStruck.transform.GetComponent<Rigidbody>().velocity = (offset * 20.0f);
}
offset = objectStruck.transform.position - player.transform.position;
if(offset.magnitude > 0.1f){
//Debug.Log ("Second if");
offset = offset.normalized;
player.GetComponent<CharacterController>().Move (offset*Time.deltaTime * 20.0f);
}
}
}
The first else if inside the (objectStruck != null) if statement is where the problem lies. I’ve checked numerous times, and it looks like the player’s totalWeight is 30, and the object’s RigidBody’s mass is also 30. So, the last else if (where the two are equal) should be running, but it’s the second one (where the object weighs less than the player) that runs.
I know that the code could use a lot of optimization, like the GetComponents or maybe combining the two together into a single function, but I want to get it functional before cleaning it up and optimizing.
Any ideas why my if statement isn’t returning properly?