- Home /
A transform.position to act as a Vector3
I have been looking high and low of how to have my fireball that hits the enemy, it will transform on the GameObject. I succeeded, the only problem is that when the enemy moves, the fireball or I mean to be specific, the particle does not follow along with the enemy it instead just spawns in the previous location that the enemy was last at when it made contact. Which in fact just typing out how my script works gives me an idea of how to fix the issue, but need more help.
I was thinking that since I have Random.Range and rigidbody.addforce, somehow could I stop it by someway because every time the object is instantiated and it hits the enemy it continues to push on forward, but in some cases when I used Rigidbody.velocity it would do what I was saying in the previous paragraph.
My Main goal that I may think solve the problem unless you have a better idea :D. Is there a possible way of having a transform.position act as a vector3 as well as transform.rotation act as one as well.
But anyway my script:
#pragma strict
var FireBallSpawn : GameObject;
var FireBall : Transform;
var Enemy : GameObject;
var EnemyPosition : Transform;
var Character : Transform;
var Speed = 100;
var DestroyTime = 10;
var DelayTime : float = 0.5f;
var NextFireBall : float = 0.0f;
function Update ()
{
if(Input.GetButtonDown("Fire1")&& Time.time > NextFireBall)
{
NextFireBall = Time.time + DelayTime;
var FireBallDirection : Vector3 = Character.TransformDirection(0, 0, 10);
var clone = Instantiate(FireBallSpawn, Character.position, Character.rotation);
clone.rigidbody.AddForce(FireBallDirection*Speed);
Destroy(clone, DestroyTime);
}
}
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Enemy")
{
var rotationspeed : float = Speed*Time.deltaTime;
FireBall.transform.parent = Enemy.transform;
FireBall.rigidbody.velocity = EnemyPosition.position;
}
}
Answer by Dzoni94 · Aug 05, 2014 at 08:32 AM
Rigidbodies get affected by physics regardless of their parent, meaning that even if your fireball is parented to the enemy, it will still be affected by gravity and will fall down etc. Solution is to destroy the rigidbody component on hitting the enemy. You can do this by simply adding Destroy(FireBall.rigidbody); inside the OnTriggerEnter function after checking if the trigger was the enemy.
Thank you so much, like this problem has been bugging me for 2 days now. Here it is if anyone is curious to what I did with his statement:
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Enemy")
{
Destroy(FireBall.rigidbody);
FireBall.transform.parent = Enemy.transform;
}
}
Also I later discovered in the context of what I was trying to put down in regards of the Transform.position and Transform.rotation already acts like a Vector 3 and Quaternion.Identity anyway. $$anonymous$$y problem here was, that force was still being applied to the particle effect, while parenting with the GameObject
at the same time. So getting rid of the rigid body itself will stop the force of the particle and rather act as a normal transform when parenting with an object. :D