addforce from grenade explosion in local coordinates
I thought I had this script perfect...I detect where the grenade has landed in relation to the enemy as far as front of, back of, left or right - then did addforce to blow the enemy in opposite direction as well as trigger the appropriate animation for the direction he falls. BUT......it only worked if the enemy is facing up the Z world axis, if he is facing any other direction it all goes haywire. i have tried the DOT and addRelative force and InverseTransformPoint,TransformPoint and everything on the web I can find. I simply want to know which side of the enemy the grenade is exploding in relation to which way he is facing so I can addforce in that direction. here is just ONE of my non working scripts. I have tried dozens of combos concerning the addforce and position detecting but I guess I haven't hit on the right pairing yet. I may just have to go with trigger colliders on all 4 sides of him and detect that way because this is driving me insane. PLEASE HELP! PRETTY PLEASE. The first part is just to decide if the grenade is more to the side or back/front since the grenade is most likely not going to fall perfectly along any one axis. then, if the grenade is within 3 units he iis effected by the explosion
using UnityEngine;
using System.Collections;
public class explosionTestBabe : MonoBehaviour {
public Transform target;
public GameObject targetObject;
Animator ani;
public bool lefts;
public bool rights;
public bool front;
public bool back;
public bool triggered;
public float thrust;
public Rigidbody rb;
public float newDrag;
void Start(){
ani = GetComponent<Animator> ();
rb = GetComponent<Rigidbody>();
rb.drag = newDrag;
}
void FixedUpdate () {
var relativePoint = transform.InverseTransformPoint(target.position);
if (relativePoint.z < 0.0&& triggered == false) {
front = false;
back = true;
targetObject.SetActive (true);
} else if (relativePoint.z > 0.0&& triggered == false) {
front = true;
back = false;
targetObject.SetActive (true);
}
if (relativePoint.x < 0.0&& triggered == false) {
lefts = true;
rights = false;
targetObject.SetActive (true);
} else if (relativePoint.x > 0.0&& triggered == false) {
lefts = false;
rights = true;
targetObject.SetActive (true);
}
if(Vector3.Distance(transform.position,target.position) < 3){
if(lefts == true && front == true && triggered == false){
if(relativePoint.x > relativePoint.z)
Right ();
if(relativePoint.x < relativePoint.z)
Back ();
}
if(lefts == true && back == true && triggered == false){
if(relativePoint.x > relativePoint.z)
Forward ();
if(relativePoint.x < relativePoint.z)
Right ();
}
if(rights == true && front == true && triggered == false){
if(relativePoint.x > relativePoint.z)
Left ();
if(relativePoint.x < relativePoint.z)
Back ();
}
if(rights == true && back == true && triggered == false){
if(relativePoint.x > relativePoint.z)
Left ();
if(relativePoint.x < relativePoint.z)
Forward ();
}
}
}
void Back(){
Debug.Log ("fall back");
triggered = true;
ani.SetTrigger ("flyback");
Vector3 direction = rb.transform.position -target.transform.position;
rb.AddForceAtPosition(direction.normalized, transform.position);
triggered = false;
lefts= false;
rights= false;
front= false;
back= false;
triggered = false;
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Forward(){
Debug.Log ("fall forward");
triggered = true;
ani.SetTrigger ("flyback");
Vector3 direction = rb.transform.position - target.transform.position;
rb.AddForceAtPosition(direction.normalized, transform.position);
triggered = false;
lefts= false;
rights= false;
front= false;
back= false;
triggered = false;
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Left(){
Debug.Log ("fall left");
triggered = true;
ani.SetTrigger ("flyback");
Vector3 direction = rb.transform.position - target.transform.position;
rb.AddForceAtPosition(direction.normalized, transform.position);
triggered = false;
lefts= false;
rights= false;
front= false;
back= false;
triggered = false;
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Right(){
Debug.Log ("fall right");
triggered = true;
ani.SetTrigger ("flyback");
Vector3 direction = rb.transform.position - target.transform.position;
rb.AddForceAtPosition(direction.normalized, transform.position);
triggered = false;
lefts= false;
rights= false;
front= false;
back= false;
triggered = false;
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
private IEnumerator DelayAfterBoom(){
yield return new WaitForSeconds(4);
moveToExplosion.x = 0;
moveToExplosion.y = 0;
moveToExplosion.z = 0;
ani.ResetTrigger ("flyback");
ani.SetTrigger ("idle");
triggered = false;
lefts= false;
rights= false;
front= false;
back= false;
targetObject.SetActive (false);
Debug.Log ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
StopAllCoroutines ();
}
}
Answer by shadowpuppet · Feb 18, 2018 at 07:06 PM
Heck with it. I went with plan B. put skinny box gameObjects as children of the enemy on all 4 sides and used their transform to calculate which way to fly
public float timeWhileJumping = 2f ;
public Transform target;
public Transform left;
public Transform right;
public Transform front;
public Transform back;
public float R;
public float L;
public float B;
public float F;
public float lowestValue;
public GameObject targetObject;
Animator ani;
public bool triggered;
public float thrust;
Rigidbody rb;
public float newDrag;
public float player_relative_to_enemy;
void Start(){
ani = GetComponent<Animator> ();
rb = GetComponent<Rigidbody>();
rb.drag = newDrag;
}
void Update () {
R = Vector3.Distance (target.transform.position, right.position);
L = Vector3.Distance (target.transform.position, left.position);
B = Vector3.Distance (target.transform.position, back.position);
F = Vector3.Distance (target.transform.position, front.position);
lowestValue = Mathf.Min (R, L, F, B);
}
void FixedUpdate(){
if (Vector3.Distance (transform.position,target.position) <= 3 && triggered == false) {
if (lowestValue == R){
Right ();
rb.AddRelativeForce(Vector3.right * -thrust);
}
if (lowestValue == L){
Left ();
rb.AddRelativeForce(Vector3.right * thrust);
}
if (lowestValue == B){
Back ();
rb.AddRelativeForce(Vector3.forward* thrust);
}
if (lowestValue == F){
Forward ();
rb.AddRelativeForce(Vector3.forward * -thrust);
}
}
}
void Back(){
Debug.Log ("fall forward");
ani.SetTrigger ("flyback");
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Forward(){
Debug.Log ("fall back");
ani.SetTrigger ("flyback");
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Left(){
Debug.Log ("fall left");
ani.SetTrigger ("flyback");
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
void Right(){
Debug.Log ("fall right");
ani.SetTrigger ("flyback");
targetObject.SetActive (false);
StartCoroutine(DelayAfterBoom());
}
private IEnumerator DelayAfterBoom(){
yield return new WaitForSeconds(timeWhileJumping);
target.transform.position = new Vector3 (0, 0, 0);
ani.ResetTrigger ("flyback");
ani.SetTrigger ("idle");
triggered = false;
targetObject.SetActive (false);
StopAllCoroutines ();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612152900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add force forward to a ball using local coordinates doesn't work as I wanted 0 Answers
Unity 2017.1.0f3 AddRelativeForce Undefined error 1 Answer
AddForce on local transform 0 Answers
Rigidbody AddForce(transform.forward) vs Rigidbody.AddRelativeForce(transform.forward) ? 1 Answer
Best ads for mobile game? 1 Answer