- Home /
Need Help for AirPlane Script
I have written below script for the Airplane this script allows airplane to move right or left and up and down but I want air plane to move forward and back also ......May I get help for the same....
using UnityEngine; using System.Collections;
public class MoveAirPlane: MonoBehaviour {
public float _init_x = 0.0f; //inital x position
public float _init_y = 0.0f; //initial y position
public float _horizontal_extent = 20.0f; //max horizontal distance achieved on each side
public float _vertical_extent = 10.0f; //max vertical distance achieve on each side
public float varspeed = 50.0f;
// Use this for initalizing setting
/*void initSettings()
{
}*/
// Use this for initialization
void Start () {
// initSettings();
}
// Update is called once per frame
void Update () {
Quaternion temp_rotate0 = new Quaternion((Input.GetAxis("Vertical") / 4) * -1, 0.0f, (Input.GetAxis("Horizontal") / 4) * -1, 1.0f);
//set the new rotation with a tween so that the craft returns back to normal in a slow motion
transform.rotation = Quaternion.Lerp(transform.rotation, temp_rotate0, 0.05f);
//0.05f will limit the rotation to maximum 45 degrees on each side, 1 will make it 90
transform.position = new Vector3(Mathf.Clamp(transform.position.x, _init_x - _horizontal_extent, _init_x + _horizontal_extent),
Mathf.Clamp(transform.position.y, _init_y - _vertical_extent, _init_y + _vertical_extent), transform.position.z);
transform.Translate(Input.GetAxis("Horizontal") * 50, Input.GetAxis("Vertical") * 50, 0, Space.World);
rigidbody.AddForce(0,0,5 * varspeed);
}
}
Could you just apply the same logic for X and Y to the Z axis ?
O ya I just need to do it for z axis ..I am so dumb !!! Thanks a lot
glad to help :D. annoying that this question shall remain flagged as unanswered....
timsk I am new to unity answers what should I do to say its correct answers ...
Answer by timsk · Sep 20, 2011 at 09:56 AM
apply the same logic you have for the X and Y axis to the Z axis.
(I'm pretty new as well, just mark this as the correct answer i guess)
This is what I have done .... Check this code but listen I just want it to move forward and not the backward... Only want " _init_z + _foward_extent " to work and not the $$anonymous$$us one then what should I write ins$$anonymous$$d of that ?
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, _init_x - _horizontal_extent, _init_x + _horizontal_extent),
transform.position.y,
$$anonymous$$athf.Clamp(transform.position.z, _init_z - _forward_extent, _init_z + _forward_extent));
well the way i would do it is define my own input and not give it a negative button. For instance verticals input has positive "W" and negative "S". I haven't actually tried this, but it seems like it should work.
I got it ... .Thanks $$anonymous$$athf.Clamp(transform.position.z, _init_z, _init_z + _forward_extent));
Your answer
Follow this Question
Related Questions
Script Execution Order removes scripts on apply 1 Answer
Can I make an array of scripts? 3 Answers
How to start with camera and Airplane for the game ? 1 Answer
Objectives based on object appear. 2 Answers
How To Add A FX When I Teleport. 0 Answers