- Home /
How to Have Projectile Reset to Initial Position Relative to Parent
I have an enemy that fires projectiles during it's animation, and the projectiles reset their position once the animation is complete. I've pretty much worked everything out, except the position the projectile resets to is relative to the entire scene, when i'd prefer it be relative to the object the projectile is a child of.
(self is the projectile game object, and body is the enemy game object)
public class StiltRanged : MonoBehaviour {
public Rigidbody bullet;
public Transform player;
public GameObject self;
public GameObject body;
public Vector3 moveDirection;
Vector3 initialPosition;
Quaternion initialRotation;
// Use this for initialization
void Start () {
initialPosition = new Vector3(0, 0, 0);
initialRotation = new Quaternion(0, 0, 0, 0);
}
// Update is called once per frame
void Update () {
if (self.activeInHierarchy == true) {
bulletFire();
}
if (self.activeInHierarchy == false) {
bulletReturn();
}
}
void bulletFire() {
moveDirection = player.transform.position - body.transform.position;
bullet.AddForce(moveDirection.normalized * 20f);
}
void bulletReturn() {
self.transform.position = initialPosition;
self.transform.rotation = initialRotation;
}
}
Are your trying to reset the position of "bullet" or "self"? In your code there is a Rigidbody bullet, and a Gameobject self. From your query it seems like you are trying to reset the projectile position which should be the "bullet", however, in the void bulletReturn() you are parsing values to "self". One more question, does your character(enemy) have movement? If that's the case you would need to get updated position of your enemy ins$$anonymous$$d of a default..
ah, my bad, "self" is the game object of the projectile, "bullet" is the rigidbody component of that same game object, and yes the enemy has movement, also the script isn't attached to the projectile itself because it needs to know if the projectile is active or not, if that helps.
Answer by Trevdevs · Jun 28, 2017 at 07:53 AM
Remember that position and rotation are the global ones in the scene if you want to access the specific objects current position and rotation its best to use localrotation
agreeing with the question ask by neosca not sure about the self part or the bullet but it should be
transform.localposition and or transform.localrotation
this may help you to
https://docs.unity3d.com/ScriptReference/Transform-localRotation.html
Your answer
Follow this Question
Related Questions
AddForce/ForceMode.Impulse 1 Answer
how to stop the camera to follow the player on his jump movement ? 2 Answers
Smooth Camera/Object Movement 1 Answer
AI Movement Direction 0 Answers