- Home /
Launched arrow sticks to target rigidbody...but causes target to spin, slide, and vibrate?
Hi everyone, I'm pretty new to Unity and scripting in general so I apologize in advance for my terrible syntax...
My game has a 3rd person character who shoots a rigidbody arrow at a rigidbody target. A script attached to his camera determines shooting direction and then instantiates the arrow, and adds force to it.
The arrow target (called 'enemy') has a script that looks for when an object tagged "arrow" collides with it. When it does, the enemy cuts the arrow's physics and parents the arrow to it.
The only problem is that when it does this, the enemy begins to slide along the ground in the direction the arrow was flying. If a second arrow is shot, the enemy begins to tumble about, sometimes even slowly floating into the air.
I think that for some reason the arrows' vectors are permanently applying force to the enemy rigidbody, and I can't figure out why.
Thank you in advance!
Here is the script that launches the arrow rigidbodies:
//Attach to camera
#pragma strict
// Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
//var projectile_nonRigid : GameObject;
var hero : Transform;
var distance = 10;
var launchHeight = 1;
var forceMultiplier = 2000;
var arrowCount : int = 50;
private var heroVelocity = Vector3.zero;
private var lastPosition = Vector3.zero;
private var inheritedForce = forceMultiplier / 33.3333;
function Update () {
//Establish vectors that read input and determine direction to launch arrow.
var cursorVector = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
var compensateVector = Vector3(0, -1, 0); //temporary inpute compensation
cursorVector = Camera.main.ScreenToWorldPoint(cursorVector);
heroVelocity = ((hero.position - lastPosition) / Time.deltaTime);//determine the hero velocity
lastPosition = hero.position; //to use for shooting while
//moving fast (galloping)
if (Input.GetButtonUp("Fire1") && arrowCount > 0) {
var clone = Instantiate(projectile, hero.position, Quaternion.identity) as Rigidbody;
clone.isKinematic = false; //not sure if this line is necessary.
clone.transform.LookAt(cursorVector + compensateVector);
clone.position.y = hero.position.y + launchHeight;
//Debug.Log(cursorVector);
clone.rigidbody.AddForce(clone.transform.forward * forceMultiplier);
clone.rigidbody.AddForce(heroVelocity * inheritedForce);
arrowCount -= 1;
}
}
And here is the script that the target 'enemy' has that looks for arrow collision:
//Code provided by unity forums member aldonaletto:
var depth: float = 0.30; // how deep the arrow will enter the body
function OnCollisionEnter(col: Collision){
if (col.transform.tag == "Arrow"){ // only for objects tagged "Arrow"
col.rigidbody.isKinematic = true; // stop the arrow's physics
col.transform.Translate(depth * Vector3.forward); // move the arrow deep inside
col.transform.parent = transform; // stuck the arrow to the enemy
}
}
Answer by robertbu · Feb 09, 2014 at 07:00 AM
Even though you made the arrow a child, there is still a collision going. Try first just turning off the collider on the arrow. My experience with collision is limited, so I always have to play to get things to work the way I want. Beyond turning off the collider, you could use a FixedJoint instead. Physics.IgnoreCollision() may also work (though I've never tried it in this situation).
col.collider.enabled = false;
Your recommendation to use Physics.IgnoreCollision() worked perfectly, thank you!
Physics.IgnoreCollision(col.collider, transform.collider);
Your answer
![](https://koobas.hobune.stream/wayback/20220613132942im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody controller with sticky walls 0 Answers
How to calculate projectile (baseball) distance with drag factor 1 Answer
How to fix Ethan ragdoll 0 Answers
Controll 3D Rigidbody Projectile with Joystick 1 Answer
Very intermittent Collsion with grenade projectile and room with mesh collider. 1 Answer