- Home /
Asteroid disappears for no reason
For my space shooter game I need to make some asteroids move towards the player.
I've created the asteroid, made it rotate and destroy on impact.
But when I add my movement script for it, it simply just disappears when testing the game.
This is my movement script:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed; //public float tilføjes så værdien kan ændres i selve unity istedet for at være kodebaseret
void Start ()
{
rigidbody.velocity = transform.forward * speed; //transform forward er bevægelsen langt z-aksen.
}
}
Ignore the danish notes, please.
im not familiar with C# but shouldnt you state what "speed" is? so far i only see that you use your float speed in your velocity but i cant see how much that speed is?
Answer by wibble82 · Apr 30, 2014 at 11:03 PM
I suspect it's just going off screen, unless some other code is killing it. Best bet - pause the game as soon as it runs, find the object in the scene view and see where it's gone!
I found the problem.
It's moving along the z axis, which in my case, is the blue vector in this picture: http://i.imgur.com/hb9DtNw.png
I want to move it along the green vector, but how can I achieve this?
Do I replace the transform.forward with something similar?
yup - that or rotate your object. transform.forward will push it along its own z axis. transform.right will push it along its own x axis. transform.up will push it along its own y axis. Or if you want to work in world space (i.e. ignore the orientation of the object), there's Vector3.up, Vector3.right and Vector3.forwards.
Answer by KevLoughrey · Apr 30, 2014 at 10:29 PM
It might be moving off camera when Start() is called? Try logging its position to the console to see what's happening with it, or reducing the speed.
I found the problem.
It's moving along the z axis, which in my case, is the blue vector in this picture: http://i.imgur.com/hb9DtNw.png
I want to move it along the green vector, but how can I achieve this?
Do I replace the transform.forward with something similar?
Answer by giantkilleroverunity3d · May 01, 2014 at 07:48 PM
Speed or wrong direction is what I have found to be the first questions to ask oneself since this is a dynamic graphics environment. From 'One who has lost many in play...'
Your answer
![](https://koobas.hobune.stream/wayback/20220613143947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script to change colour makes GUItext invisible. Any idea why? 3 Answers
cannot show my hazard in space shooter 0 Answers
SphereCollider on lfoot is not recognized 0 Answers
Asteroid wrapping effect. 1 Answer
Why are all the particles in my particle system disappearing when it's off screen? 1 Answer