Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by shadowpuppet · Feb 18, 2018 at 05:18 PM · addforcelocalspaceaddrelativeforceworld coordinates

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 ();
     }
 }
 
 
         
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 ();
     }
 }
 
 
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges