- Home /
Project space shooter problem
I was watching the projects of unity and i decided to make project space shooter. my space ship, background, lights were ready. Then I make the script to move the space ship as was told in the video. after it was completed, i decided to test it. my space ship was moving but when i moved it, it return to his original position and cannot move my space ship after a certain distance. Help me with this please !!! The script was this :-
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary {
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
Did you set the speed, tilt and boundary correctly in the inspector? I looked at the original source of the example and your code should work O$$anonymous$$, so maybe you have some components with a different setup.
Could you clarify the issue? Is your ship returning to origin after a few moments of movement? I'm learning Unity myself and am currently on this tutorial but I had no issues with this part and our code looks identical.
Answer by komodor · Jun 23, 2015 at 06:06 PM
if you want to use physics in the game, then you should probably AddForce to the rigidbody (http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html) instead of setting velocity and position of rigidbody
if you want to move the gameobjects yourself then you don't need rigidbody at all and you should affect Transform.position of the gameobject
Answer by tbrosh23 · Jul 01, 2015 at 04:08 PM
I had the same problem! It is because you did not change the boundary values after you wrote them into your code.
Answer by Trys10Studios · Aug 16, 2015 at 08:56 AM
Yes the boundary has to be reset in the scene view, or at least that's what I did. Thank you for the answer!
Answer by Darkskyb2 · Nov 03, 2016 at 06:17 PM
private Rigidbody rb;
void Start ()
{
rb = GetComponent <Rigidbody> ();
}
Include it, so you dont have to put GetComponent (); every time you want to call the rigidbody. just need to put this (Ex: rb.position)
Your answer

Follow this Question
Related Questions
is it always necessary to use an object? 1 Answer
How do Code this? AddScore is outdated. 2 Answers
Unity 5 3D Space Movement Issues 0 Answers
How Can I change between functions after a certain time? 1 Answer
Hello, I am working on a spaceship game and can't get my score to show or pop up. I need help. 0 Answers