yakawa
March 11, 2015, 5:12pm
1
hi i’m trying to make my player shot a bullet when “q” is pressed and when he press “q” again he will teleport to the bullet position . Im using this code : (the problem is on the update function )
public class RobotController : MonoBehaviour {
public float nextFire;
public float fireRate;
public bool tp= false ;
public GameObject projectile;
public Transform spawn;
public float maxSpeed = 10f ;
bool facingRight = true;
public float lifeTime = 3f;
public Transform proj;
Animator anim ;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y,0);
if (move < 0 && facingRight)
Flip ();
else if (move > 0 && !facingRight)
Flip ();
}
void Update(){
if (Input.GetKey ("mo") && tp == false && Time.time > nextFire ) {
Spawn ();
nextFire = Time.time + fireRate;
tp = true;
}
if (Input.GetKey ("q") && tp == true) {
gameObject.transform.position = new Vector2 ( proj.transform.position.x,proj.transform.position.y);
tp = false;
}
}
void Flip (){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Spawn (){
GameObject clone;
clone = (GameObject) Instantiate (projectile, spawn.position, spawn.rotation);
Destroy (clone, lifeTime);
projectile = clone;
}
}
projectile is the prefab of my bullet , the transform proj = projecile . the problem that instead of tp on the bullet the player tp on the first position of the projectile setted in the prefab wats wrong ?
Oribow
March 11, 2015, 7:47pm
2
You asign the position of the bullet to projectile, but teleport the player to proj, wich was never asigned in your code? Rename projectile to proj at:
projectile = clone
yakawa
March 13, 2015, 8:59pm
3
it still didn’t work . But I changed the script and it worked here the code if you want to know and thanks for the help ^^
public float nextFire;
public float fireRate;
public float nextTp;
public float tpRate;
public GameObject projectile;
public Transform spawn;
public float maxSpeed = 10f ;
bool facingRight = true;
public float lifeTime = 3f;
GameObject tmp;
float projInstanceMouseX;
float projInstanceMouseY;
Animator anim ;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y,0);
if (move < 0 && facingRight)
Flip ();
else if (move > 0 && !facingRight)
Flip ();
}
void Update(){
if ((Input.GetKey ("q")) && (tmp == null) && (Time.time > nextFire || Time.time < 2f)) {
Debug.Log("instanciate");
nextTp = Time.time + tpRate;
Spawn ();
nextFire = Time.time + fireRate;
} else if ((Input.GetKey ("q")) && (Time.time > nextFire && Time.time > nextTp ) && (tmp != null)) {
gameObject.transform.position = new Vector2 (tmp.transform.position.x, tmp.transform.position.y);
Destroy(tmp);
nextFire = Time.time + fireRate;
Debug.Log("destroy");
} else if (tmp != null) {
tmp.rigidbody2D.velocity = new Vector2 (projInstanceMouseX * Time.deltaTime * maxSpeed, projInstanceMouseY * Time.deltaTime * maxSpeed);
}
}
void Flip (){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Spawn (){
projInstanceMouseX = Input.mousePosition.x;
projInstanceMouseY = Input.mousePosition.y;
tmp = (GameObject)Instantiate (projectile, spawn.position, spawn.rotation);
Destroy (tmp, lifeTime);
}
}