I'm working on a simple script that will create an object a couple meters in front of the character. For now I've been using a default sphere. I tried to do it using transform.TransformDirection, but it seems to always use the character's original position for the transform. No matter where the character moves, the sphere is always created in relation to where the character started at the beginning of the level, instead of where it is at the moment. Let me know if I'm missing anything.
Here's the code:
using UnityEngine; using System.Collections;
public class spawnPrefab: MonoBehaviour {
public Transform sphere;
float lastCast = -10f;
float castTime = .5f;
void Start () {
}
void Update () {
if(Input.GetButton("Fire1")){
if(Time.time - lastCast > castTime){
spawn();
lastCast = Time.time;
}
}
}
void spawn(){
Transform currentPos = transform;
float currentY = transform.position.y;
Vector3 newPos = currentPos.TransformDirection(
Vector3.forward*3);
newPos.y = currentY;
Instantiate(sphere, newPos, transform.rotation);
}
}