spaceship turns down when applied script

hello i applied scripts for to control the spaceship but when i press play it turns down instead of moving
Here are my script Title=PlayerController (SpaceShipController)
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”);
var movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
var rigidbody = GetComponent ();

rigidbody.velocity = movement * speed;

rigidbody.position = new Vector3 (
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);

rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}

First, please put code tags around your code (easiest way to use that is to use the Insert button, between the movie and the floppy disk in the edit toolbar).

Next, I wouldn’t expect it to move when you press Play. Movement in this script is based on inputs, and you’re probably not holding the input keys down when you press Play.

Next, if you’re going to be directly setting the position and velocity, then you probably don’t need to be using RigidBody at all. Just remove the Rigidbody and manipulate the .transform instead; it’s much easier. (Unless for some reason you do need physics, but in that case, you shouldn’t be assigning to .position.)

Finally, if the ship “turns down” I will guess you mean it rotates to a certain orientation — well, you are directly assigning the rotation in the last line, based on “tilt” which you never change in this script, so it’s not surprising that it should rotate to whatever angle the tilt field specifies.

1 Like

i hold up key when i press play but still didnt move and what is setting posistion and velocity directly? lol i dont undertand and the tilt is 0

and also do i need to learn anything before writing this script? cause i didnt understand this script . when ever i tried to read i only understand this line “using unity engine” other than that nothing i can understand and also this is the first time iam writing it in c# . before i used javascript which is just 8-15 lines for moving an object but in c# it almost 30 lines for just moving a spaceship

Yes. You need to learn things before attempting to write a script like this.

Start by going through tutorials, and make sure you understand what you’re doing — don’t just grab a script from somewhere, throw it in, and hope for the best. And when you do write a script, start with an empty MonoBehaviour, and then add things one at a time, testing after each step. Don’t go straight for rotation plus movement plus physics, and then wonder why the result isn’t what you imagined.

Programming isn’t magic; it’s just thinking clearly, being very clear about what you’re trying to say, and being consistent and correct with things like syntax. (Incidentally, these same skills in all arenas of life, such as writing clear, understandable forum posts.)

1 Like

i think my spaceship not moving because iam doing it on android . its also says setup sdk for control