- Home /
Help with initiating an automated sequence
Hi, I have a game in which you control a catapult. Currently the player can rotate the "arm" of the catapult and the game launches the projectile if the arm reaches a certain angle. However, as this is an educational momentum-based game, I need the angle of the catapult arm to affect the projectile's velocity. To get to doing this I need the player to be able to initiate a launch sequence for the catapult arm, storing the rotation of the arm at the point of initiation so it can be used later. The problem I am having is setting up a script for launch. It needs to do the following:
Stop accepting input from the up and down arrow keys temporarily as they affect the rotation script
Only require the space bar to be pressed once in order to trigger the launch
Automate the movement of the arm from whatever rotation it is at, up to the 90 degree mark
The actual trajectory of the projectile is done elsewhere and is initiated when the arm reaches the 90 degree mark in its x axis rotation.
The rotation script for the catapult arm is as follows:
var speed : float = 5.0f; // meters per second function FixedUpdate() { if(Input.GetKey ("up") && transform.rotation.eulerAngles.x < 85) { transform.rotation.eulerAngles.x += speed; } if(Input.GetKey ("down") && transform.rotation.eulerAngles.x > 10) { transform.rotation.eulerAngles.x -= speed; }
}
Any help in setting up the launch sequence script would be greatly appreciated!
Many thanks to such a great community
Answer by clfischer · Mar 28, 2012 at 10:37 AM
Would this work?
var launching : bool = false;
var adjustmentSpeed : float = 5.0f; // degrees per second
var launchSpeed : float = 100.0f; // degrees per second
var launchAngle : float; // degrees
function Awake() {
transform.rotation.eulerAngles.x = launchAngle;
}
function FixedUpdate() {
if (!launching) { // no launch in progress
if (Input.GetKey ("up") && transform.rotation.eulerAngles.x < 85) {
transform.rotation.eulerAngles.x += adjustmentSpeed*Time.deltaTime;
}
if (Input.GetKey ("down") && transform.rotation.eulerAngles.x > 10) {
transform.rotation.eulerAngles.x -= adjustmentSpeed*Time.deltaTime;
}
if (Input.GetKey ("space")) {
launchAngle = transform.rotation.eulerAngles.x;
launching = true;
}
} else { // launch has been initiated
if (transform.rotation.eulerAngles.x < 90) { // Swing catapult arm until it reaches 90 degrees.
transform.rotation.eulerAngles.x += launchSpeed*deltaTime;
} else { // Launch projectile when the arm has reached 90 degrees.
Launch();
}
}
}
function Launch() {
// Eg. Instantiate projectile and give it the appropriate velocity
// based on the value of launchAngle.
}
Remember to reset the launching
flag to false when you're ready to for the user ta make adjustments and launch again.
Your answer

Follow this Question
Related Questions
Rotate object after swipe according to swipe direction 0 Answers
PS3 Analogue Stick Axis Values 1 Answer
Can i change position depending on another object axis? 2 Answers
Rotating object with a mouse movement 0 Answers
Drag object in a circle path 1 Answer