I would like to ask a question for my rocket game:
PROBLEM:
My rocket did not fly, it stayed as a static picture in one place.
Every time the airplane shot a rocket clone, and it just stayed at where I pressed the key, it never fly.
I followed the tutorial book about how to make a simple airplane and rocket game in Unity 3D.
A 2D style game, but the objects beside from the background are all in 3D.
I would like to ask is there any problem with the script below? It is a C#script attached to the object rocket. I have made it a Prefab, and is already attached to the airplane.
-----( C#script attached to the object rocket)--------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu(“MyGame/Rocket”)]
public class Rocket : MonoBehaviour
{
public float m_speed = 10;
public float m_liveTime = 1;
public float m_power = 1.0f;
protected Transform m_trasform;
// Use this for initialization
void Start()
{
m_trasform = this.transform;
}
// Update is called once per frame
void Update()
{
m_liveTime -= Time.deltaTime;
if (m_liveTime <= 0)
Destroy(this.gameObject);
m_trasform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));
}
}
--------------( C#script attached to the object airplane)--------------------------------------
[AddComponentMenu(“MyGame/Player”)]
public class Player : MonoBehaviour
{
public float m_speed = 3;
public Transform m_rocket;
// Start is called before the first frame update
protected Transform m_transform;
void Start()
{
m_transform = this.transform;
}
// Update is called once per frame
void Update()
{
float movev = 0;
float moveh = 0;
if(Input.GetKey(KeyCode.UpArrow))
{
movev -= m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.DownArrow))
{
movev += m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
moveh += m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.RightArrow))
{
moveh -= m_speed * Time.deltaTime;
}
this.m_transform.Translate(new Vector3(moveh, 0, movev));
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
Instantiate(m_rocket, m_transform.position, m_transform.rotation);
}
}
},I would like to ask a question for my rocket game:
PROBLEM:
My rocket did not fly, it stayed as a static picture in one place.
Every time the airplane shot a rocket clone, and it just stayed at where I pressed the key, it never fly.
I followed the tutorial book about how to make a simple airplane and rocket game in Unity 3D.
A 2D style game, but the objects beside from the background are all in 3D.
I would like to ask is there any problem with the script below? It is a C#script attached to the object rocket. I have made it a Prefab, and is already attached to the airplane.
-----( C#script attached to the object rocket)--------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu(“MyGame/Rocket”)]
public class Rocket : MonoBehaviour
{
public float m_speed = 10;
public float m_liveTime = 1;
public float m_power = 1.0f;
protected Transform m_trasform;
// Use this for initialization
void Start()
{
m_trasform = this.transform;
}
// Update is called once per frame
void Update()
{
m_liveTime -= Time.deltaTime;
if (m_liveTime <= 0)
Destroy(this.gameObject);
m_trasform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));
}
}
--------------( C#script attached to the object airplane)--------------------------------------
[AddComponentMenu(“MyGame/Player”)]
public class Player : MonoBehaviour
{
public float m_speed = 3;
public Transform m_rocket;
// Start is called before the first frame update
protected Transform m_transform;
void Start()
{
m_transform = this.transform;
}
// Update is called once per frame
void Update()
{
float movev = 0;
float moveh = 0;
if(Input.GetKey(KeyCode.UpArrow))
{
movev -= m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.DownArrow))
{
movev += m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
moveh += m_speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.RightArrow))
{
moveh -= m_speed * Time.deltaTime;
}
this.m_transform.Translate(new Vector3(moveh, 0, movev));
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
Instantiate(m_rocket, m_transform.position, m_transform.rotation);
}
}
}