- Home /
Issue with adding force and lerping position? Possible unity bug?
The Bouys in my boat game aren't acting correctly when shot. When the rigid body is normally pushed by another rigidbody it will be pushed, and due to this script:
using UnityEngine;
using System.Collections;
public class buoyScript : MonoBehaviour {
Vector3 startPos;
public float bounceBack = 0.5f;
// Use this for initialization
void Awake () {
startPos = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
transform.position = Vector3.Lerp (transform.position, startPos, bounceBack);
}
}
And this script for the shots:
using UnityEngine;
using System.Collections;
public class shotScript : MonoBehaviour {
public float shotVel = 10;
public float hitVel = 10;
public int shotDam = 0;
public string tarTag = "Enemy";
public Rigidbody2D RB;
// Use this for initialization
void Start () {
RB = gameObject.GetComponent<Rigidbody2D> ();
RB.velocity = shotVel * transform.up;
}
void OnTriggerEnter2D(Collider2D col){
if (col.tag == tarTag) {
col.gameObject.SendMessageUpwards ("hurt", shotDam);
}
if(col.gameObject.GetComponent<Rigidbody2D>() != null){
var colRB = col.gameObject.GetComponent<Rigidbody2D> ();
colRB.AddForce (hitVel * transform.up, ForceMode2D.Impulse);
}
Destroy (gameObject);
}
}
Here is a video of the behavior as an example.
Notice how the boat can push the bouy and it will bounce back as programmed, but when shot it will cause it to not bounce back until the bout touches it again. The boat never sends any message to the bouy upon touch, all this is done by the bouy script and unity's built in rigidbody system, the only exception to that is the shot. Even weirder is the fact that the particles act correctly to the bouy after being shot, but the boat collides with it at a different area, showing that where the rigidbody believes the collider is, and where the collider actually is are at two different locations.
It is, but after testing, it seems that even if it isn't a child of any object it still has the same behaviors.
Can I see a picture of your heirarchy please, to see if I can recreate this?
Yeah, although the cannon balls are triggers if that matters.
I find that using the velocity and mass of the built in physics system by changing the cannon balls from triggers to normal colliders simply works better, and makes the bouys behave correctly. Either way I feel this bug has more to do with an issue with Unity than my code. I might be wrong, but i really can't figure out what I did wrong.
I can't seem to be able to figure it out. I'm sorry, I tried everything I could but I can't see why this isn't working.
Don't worry about it. I found away around it, while also making my cannon balls preform better in the process anyways. It's probably an engine bug and not problem with my code. Although I should probably see about getting a bug report submitted about this entire situation. Thanks for all of your help!
Your answer
Follow this Question
Related Questions
Vibrating GameObjects 0 Answers
Why can't my enemies walk up an incline? 1 Answer
Restrict two objects from getting too close. 1 Answer
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer
What is the best way to prevent a physics object from going through thin colliders? 2 Answers