- Home /
Script doesn't obey
This is a strange thing to say... but i think my script isn't "obeying" me. Have a look at this image:
I have an airplane. I need it to go FORWARD, but it is literally going up! As you see in the image, i want my plane to go to the circle marked between the white cubes, and it is going to the "X". I read in the docs that BLUE stands for FORWARD and GREEN stands for UP in scripting. So what is wrong with my script? I have tested a lot of different combinations of position translation but it simply doesn't work. What am i doing wrong? By the way, i am beginner at C# so i am probally missing some basic stuff here.
using UnityEngine;
using System.Collections;
public class PlaneControl : MonoBehaviour {
public float speed = 1f;
void Update()
{
transform.position = transform.position + (transform.forward * speed * Time.deltaTime);
}
}
I've copied your exact code in my project, and it works exactly as expected. Do you have any other scripts attached to that object?
my guess is, the airplane is made of several gameobjects of which the one the script is attached to is not facing forward.
it's possible that you think it does because you can toggle the gizmo between world and local view (somewhere above the game view, next to the tools)
Answer by phxvyper · Oct 24, 2016 at 09:03 PM
I commented earlier, but just made the realization that youre using transform.forward
which is dependent upon the rotation of your model.
transform.forward
is indeed, as you've put it, the "BLUE" arrow... however, in your picture, you're showing the global axes instead of the local axes. transform.forward
is a local, relative value. The solution is to rotate the forward vector using your transform's Quaternion rotation, like so:
transform.position = transform.position + ((transform.rotation * transform.forward) * speed * Time.deltaTime);
That is, of course, assuming you want it to move forward based upon the plane's rotation, and not just globally forward regardless of the planes rotation. (You would use Vector3.forward
instead of transform.forward
for that).
Answer by Bunny83 · Oct 24, 2016 at 09:05 PM
Make sure you set your Gizmo mode to "pivot" and "local":
Your answer
Follow this Question
Related Questions
unity 3.4 crashes when i get to a certain point in the character generator script? 0 Answers
How to create a UnityScript array and access the data in each cell. 1 Answer
Serialize is working in debug, but it has no effect 0 Answers
Changing a UI image sprite in scripts results in a black texture 0 Answers
Swapping animation clips at runtime 1 Answer