transform.forward problem
Hey everyone, I've been having some difficulties with a projectile script thats really been causing problems for me. I've finally managed to instantiate the projectile in the correct direction with the ray pointing in the right direction as well, but ever since I started using "transform.forward", the bullet flys in all kinds of directions. Does anyone know how to stop it from doing that?
using UnityEngine;
using System.Collections;
public class MusketBullet : MonoBehaviour {
//Variables Start___________________________________
//The explosion effect is attached to this
//in the inspector
public GameObject mudParticle;
//A quick reference.
private Transform myTransform;
//The projectiles flight speed.
private float projectileSpeed = 20;
//Prevent the projectile from causing
//further harm once it has hit something.
private bool expended = false;
//A ray projected in front of the projectile
//to see if it will hit a recognisable collider.
private RaycastHit hit;
//The range of that ray.
private float range = 1.5f;
//The life span of the projectile.
private float expireTime = 10;
//Variables End_____________________________________
// Use this for initialization
void Start()
{
myTransform = transform;
transform.Rotate(0.0f, 270.0f, 0.0f);
//As soon as the projectile is created start a countdown
//to destroy it.
Destroy (gameObject, expireTime);
}
// Update is called once per frame
void Update ()
{
//Translate the projectile in the up direction (the pointed
//end of the projectile).
myTransform.Translate(transform.forward * projectileSpeed * Time.deltaTime);
//If the ray hits something then execute this code.
Debug.DrawRay( myTransform.position, myTransform.forward * range, Color.red );
if(Physics.Raycast(myTransform.position,myTransform.forward, out hit, range) && expended == false)
{
//If the collider has the tag of Floor then..
Debug.Log( "Ray Hit (floorMud) : " + hit.collider.transform.tag );
{
expended = true;
//Instantiate an explosion effect.
Instantiate(mudParticle, hit.point, Quaternion.identity);
//Make the projectile become invisible.
myTransform.renderer.enabled = false;
}
}
}
IEnumerator DestroyMyselfAfterSomeTime()
{
//Wait for the timer to count up to the expireTime
//and then destroy the projectile.
yield return new WaitForSeconds(expireTime);
Destroy(myTransform.gameObject);
}
}
Answer by Eric5h5 · May 19, 2013 at 10:07 PM
Transform.Translate already uses the object's local space by default. Therefore it makes no sense to use transform.forward inside the Translate function. Use Vector3.forward instead, as shown in the code example for Translate in the Unity documentation.
Okay thank you, I noticed that the problem lies in the instantiation itself though, or in this case, the actual gun firing script. Thank you for your advice though (actually tried Vector3.forward and got the same result).
Answer by Bunnybomb7670 · May 19, 2013 at 09:33 PM
The transform.forward goes from the transform.forward of the object that the script is attached to unless you state something elses transform.forward, I suggest you check that the front of your bullet is facing the blue arrow and also make sure that your myTransform is also facing forward.
I actually figured out what I need to do, thank you for your help, I realized that I need to modify the gun firing script itself so that the bullet instantiates correctly.