- Home /
Fireball Script not moving in a straight line
Hi I'm new to coding and I can't figure out why my fireball isn't going straight. Here's the script:
using UnityEngine; using System.Collections;
public class Fireball : MonoBehaviour {
private Transform _forward;
private Vector3 _moveDirection;
private CharacterController _cc;
private float _fireballSpeed = 15f;
private Transform _myTransform;
private float _lifespan = 10f;
private int _damage;
void Awake() {
_myTransform = transform;
}
// Use this for initialization
void Start () {
_damage = 10;
}
// Update is called once per frame
void Update () {
_myTransform.position += _myTransform.forward * _fireballSpeed * Time.deltaTime;
_myTransform.rotation = new Quaternion( 0, 0, 0, 0);
_lifespan -= Time.deltaTime;
if(_lifespan < 0)
{
Destroy(this.gameObject);
Debug.Log( "Time destroying!" );
}
}
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Debug.Log( "Destroying!" );
}
}
Answer by Piflik · Apr 14, 2012 at 07:25 PM
The problem is Quaternion(0,0,0,0), which doesn't work as a rotation. Use Quaternion.identity (for a rotation, the sum of the 4 numbers have to be one, since you want a normalized quaternion).
Answer by yezzer · Apr 14, 2012 at 05:32 PM
Why are you storing _myTransform = transform; in Awake()? Instead:
void Update()
{
transform.position= (etc etc)
}
Sorry i'm new to scripting >.> I took out _myTranform, I still can't figure out how the fireball go in a straight line.
transform is a property that accesses a component, and requires a function call - the compiler translates transform to the following instruction:
this.get_transform()
For performance reasons, it's better to copy transform to a variable at Awake or Start, since the access to variables is faster.
Answer by JayMHelpsU · Apr 14, 2012 at 07:27 PM
Are you using a rigidbody on the object,if so make sure you have gravity switched off :)