playMaker

Author Topic: Arrow Stuck in Object  (Read 333 times)

Pandur

  • Junior Playmaker
  • **
  • Posts: 87
    • View Profile
Arrow Stuck in Object
« on: January 12, 2019, 04:06:53 AM »
Hello,

i have a littel Problem i have create a basic Bow System, with left mouse key the arrow will fly from spawnpoint and catch a rabbit and the rabbit is dead, that works fine for me but my problem is the arrow , that will not stuck in the body of the rabbit.

What have i do :
On The Arrow a fsm :
State 1 :
Raycast 2 Ignore Triggers : That function have Hit Event , Hit object and Hit Point,the Distance is on 0.25 from the raycast.

Ok the arrow fly to the rabbit (that have a Box Collider on the Spine2 bone with a FSM) the raycast see the box collider and send the event to state 2

On State 2 :
Get Tag : here i take the take from the hit object variabel (that is our rabbit) and via string compare i will see have the hit object the tag animal or not,when it is a animal we go to state 3 :

State 3 :

Send Event by Name : that send the a Event to the fsm on the rabbit (Spine bone 2) and set the kinematic to true from the arrow.Next is set Parent (here i use the hit object and use owner).

Problem : my arrow will not in the body from the rabbit the arrow is on the point from collider.

What have i make it :

i have test it out with get position from the arrow , Vector 3 add yxz with z = 0.25, set position with the Vector 3 add.that works only when i shoot the rabbit from the right site (the arrow is in the body , jippi) but from the left site the arrow is in the air.
other test with transform direction : the arrow is in the air for the rabbit.

Hope somebody have a idea at time i go crazy.... :D

ch1ky3n

  • Junior Playmaker
  • **
  • Posts: 71
    • View Profile
Re: Arrow Stuck in Object
« Reply #1 on: January 14, 2019, 10:16:40 AM »
I assume your game is in 2d, or you could just adjust it yourself if it is on 3d. So, you want to stick the arrow on the rabbit body, I suggest you do this:

1. Set the layer so the arrow is at the back of the rabbit sprite

2. set the collider is slightly behind the arrowhead, so it will give an impression of the arrow is sticking inside the body of the rabbit later on

3. When the arrow hit, regardless on how you detect it,
 - turn off the gravity scale of the arrow
 - set arrow parent to the rabbit ( if you want the arrow keep following the rabbit after it stuck)
 - set velocity of the arrow to zero

hope it helps

Pandur

  • Junior Playmaker
  • **
  • Posts: 87
    • View Profile
Re: Arrow Stuck in Object
« Reply #2 on: January 14, 2019, 11:34:21 AM »
thx for your answer i create a 3d game project.

here my problem on a pic : the arrow stuck not into the tree

my settings / States :




So a Problem is : on c# give it that function : function OnCollisionEnter(collision : Collision) { / for (var contact : ContactPoint in collision.contacts) {
                  transform.position = contact.point;
            }
            //parent arrow to object | if the object the arrow hits moves, this will make the arrow move with it
            transform.parent = collision.transform;

but when i use that c# script on my arrow the raycast detection on playmaker on that arro will not more work , see here the script :

Code: [Select]
using UnityEngine;
using System.Collections;

public class ArrowStuck : MonoBehaviour {

RaycastHit hit;
public AudioClip hitSound; //sound to play when arrow hits a surface
public LayerMask layerMask;
    public bool m_AlreadyHitSomething = false;
    public string GameObject;
    // Update is called once per frame
    void Update () {
//Only check for obstacles to stick to if the arrow is moving
if(GetComponent<Rigidbody>().velocity.magnitude > 0.5) {
CheckForObstacles();
}
else {
enabled = false;
}

}

void CheckForObstacles() {
//the length of the raycast needs to be greater the faster the arrow is traveling, in order for it to register
float myVelocity = GetComponent<Rigidbody>().velocity.magnitude;
//get length of raycast based on velocity of arrow | based on this, arrows with too little of a velocity will not stick
float raycastLength = myVelocity * 0.03f;

if(Physics.Raycast(transform.position, transform.forward, out hit, raycastLength, layerMask)) {
GetComponent<AudioSource>().Stop();
GetComponent<AudioSource>().clip = hitSound;
GetComponent<AudioSource>().Play();

//since we need to disable the boxCollider and freeze the rigidbody for the arrow to stick,
//we must add our own force to any moveable objects (objects with rigidbodies) that the arrow hits
if(hit.transform.GetComponent<Rigidbody>()) {
//add force to object hit
hit.transform.GetComponent<Rigidbody>().AddForce(transform.forward * myVelocity * 10);
}


//disable arrow's collider | if you leave the arrow's collider on, while inside another object, it may have strange effects
GetComponent<BoxCollider>().enabled = false;

//if you require the boxCollider to remain active, say for picking up the arrow, you can instead set the collider to trigger:
//GetComponent(BoxCollider).isTrigger = true;

//freeze rigidbody
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
//position arrow
transform.position = hit.point;
//parent arrow to object | if the object the arrow hits moves, this will make the arrow move with it
transform.parent = hit.transform;

//once the arrow is stuck, disable this script
enabled = false;
}
else {
//make arrows top-heavy | on arrows with little velocity (when the bow is not drawn back very far) the arrows will rotated toward the ground
Quaternion newRot = transform.rotation;
newRot.SetLookRotation(GetComponent<Rigidbody>().velocity);
transform.rotation = newRot;
}
}

void OnCollisionEnter(Collision collision)
    {
        if (m_AlreadyHitSomething)
return;
        m_AlreadyHitSomething = true;

       // if (collision.gameObject.tag == "GameObjectTag")
           // GameObjectTag = GameObject.ToString;
        {
if(GetComponent<Rigidbody>().velocity.magnitude > 5) {
if(collision.transform.tag != "Player") {
if(collision.transform.gameObject.layer != LayerMask.NameToLayer("Player")) {
GetComponent<BoxCollider>().enabled = false;

//freeze rigidbody
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
//position arrow
foreach(ContactPoint contact in collision.contacts) {
Vector3 pos = contact.point;
transform.position = pos;
}
//parent arrow to object | if the object the arrow hits moves, this will make the arrow move with it
transform.parent = collision.transform;

enabled = false;
}
}
}
}
}
}

hope somebody can tell me why the arrow stuck script not will work with playmaker raycast.



« Last Edit: January 14, 2019, 11:41:48 AM by Pandur »

ch1ky3n

  • Junior Playmaker
  • **
  • Posts: 71
    • View Profile
Re: Arrow Stuck in Object
« Reply #3 on: January 14, 2019, 11:50:30 AM »
I think Master Jean Fabre will be able to help you sir

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14244
  • Official Playmaker Support
    • View Profile
Re: Arrow Stuck in Object
« Reply #4 on: January 15, 2019, 05:45:09 AM »
Hi,

 you need to fake the penetration of the arrow into the rabbit using the velocity of the arrow, which means it will always be of the right sign.

 simply get the velocity of the arrow, and use it ( with a multiplier) as the extra offset from the contact point, the velocity contains the direction of the arrow, so you will be good for all cases.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14244
  • Official Playmaker Support
    • View Profile
Re: Arrow Stuck in Object
« Reply #5 on: January 15, 2019, 05:46:38 AM »
Hi,

 the other possibility is to make the arrow collider further back on the arrow itself, then it the contact point will be far enough for the arrow tip to penetrate the collider. I guess it's even simpler like that!

Bye,

 Jean

Pandur

  • Junior Playmaker
  • **
  • Posts: 87
    • View Profile
Re: Arrow Stuck in Object
« Reply #6 on: January 15, 2019, 01:31:59 PM »
Ah ok thx for the answer, i have test it out with the collision see here but that will not work for me with the script,how i can get the velocity of the arrow?can you give me please a littel sampel for that? big thx Jean Fabre

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4323
    • View Profile
    • jinxtergames
Re: Arrow Stuck in Object
« Reply #7 on: January 16, 2019, 12:45:41 AM »
hi.
you can use 'get velocity'
or
After collision event you can use "Get Collision Info" then you get the speed/velocity on impact

the object must have a rigidbody.
FOR HIRE:Playmaker/C#

Playmaker Samples <-------------> Easy Exporter <---------------------> TPA Actions

Pandur

  • Junior Playmaker
  • **
  • Posts: 87
    • View Profile
Re: Arrow Stuck in Object
« Reply #8 on: January 16, 2019, 11:52:02 AM »
Thx for the answer djaydino, i have get it in the states and that works fine for the rabbit.but i think i must total change the system.i write later when i have fix it.