Jump to content

User Controlled Spin Not Working


BrownBoxStudio
 Share

Recommended Posts

My script is not running as expected. The script is supposed to let a user control the spin of an object upon sit. You hold left or right and speed is added in the appropriate direction. If you switch directions the spin slows down and then switches the direction upon hitting zero. Speed is then added again and the object spins in the opposite direction. Although right now my script seems to be jumping to the "if else" and "else" statements. The script will only spin right, even if left is pressed first. The script will also only add speed while never subtracting speed no matter what button is pressed. I'm not to worried about switching directions right now; I am just working on getting the spin to slow down properly. 

I couldn't find any good information on how to use if statements in a control, I thought I set it up properly but it's late and I might have missed something. Would anyone mind taking a look at the control section of my script and seeing if there is anything apparently missing to cause the script to act like parts are being skipped? I appreciate any help; Thanks!

 

float spinRateAdjust = 1;
float spinRate = 0;
float rotStep = 1;
integer left = FALSE;
integer right = FALSE;


    control(key id, integer held, integer pressed)
    {

        if (held && (CONTROL_ROT_LEFT)) //Spin Left
        {
            if (spinRate > 0 && right == TRUE) // Slow down when switching directions.
            {
                //llOwnerSay("Spinning Left");
                //spinRate -= spinRateAdjust;
                if (spinRate <= 0)
                {
                    rotStep = 0 - rotStep;
                    right = FALSE;
                    left = TRUE;
                }
            }
            else //Apply Spin
            {
                left = TRUE;
                spin();
            }
        }
        
        else if (held && (CONTROL_ROT_RIGHT)) //Spin Right
        {
            if (spinRate > 0 && left == TRUE) // Slow down when switching directions.
            {
                //llOwnerSay("Spinning Right");
                //spinRate -= spinRateAdjust;
                if (spinRate <= 0)
                {
                    rotStep = 0 + rotStep;
                    left = FALSE;
                    right = TRUE;
                }
            }
            else //Apply Spin
            {
                right = TRUE;
                spin();
            }
        }        
    }
Link to comment
Share on other sites

Very quick thought as I'm doing stuff myself, but in the spin left, you use "rotStep *= -1", which , the first time you use it, will negate the value of rotStep, but if called subsequently, is going to flip rotStep between positive and negative values. Is this what you intend to happen? Did you instead intend to decrement the value of rotStep there to increase the spinrate?

Link to comment
Share on other sites

No, I actually thought I removed all of rotStep from the script I posted here. In the end I do plan to flip between the values because Im using llTargetOmega and just want to change the direction once speed reaches 0. Right now I'm just trying to get the speed to go down to 0 when the opposite direction is pressed but the script won't even run properly.

I'm applying everything as llTargetOmega(<0,0, rotStep>, PI/180*spinRate, spinRate); if that explains my intentions any better.

 

Link to comment
Share on other sites

OK, I have a better idea of what you are trying to accomplish. I think you might be better off trying a simpler approach inside the control event.

In a global variable, record the last value of the spin variable. ( will be 0 on startup, and presumably spin value will also be 0)

Interrogate the keys, if a right key has been pressed, increase the spin variable by 1  and possibly clamping it to a maximum, if left has been pressed, decrease the spin variable by 1, possibly clam[ping it to a minimum value.

Then, compare the new value of spin variable against the last saved value.

If less than the last value then If greater then 0, apply slowing down right spin. else apply increasing left spin

If greater than the last value then if less than 0, apply slowing down left spin else apply increasing right spin

if 0, stop spin.

finally, set the last saved value to the current value.

 ETA Just spotted the other thing, you are using && when testing for a control key being pressed, but the integer specified as the second argument to the control event is a bitfield, so you need to test for a bit being set by using & instead of testing for logical AND with &&.

 

In addition, you have commented out the line that would decrement spinRate, so you are going into a block of code where Right is TRUE and spinRate > 0, but then there is only a test for spinRate <= 0, which will never evaluate TRUE unless you first decrement the value which was > 0 to get into that block in the first place :)

Note that the user might be pressing both the left and right keys together. If so, the way your code is written at the moment, once a left keypress has been detected, there will be no subsequent test for a right key press. This makes sense in your application, but in other instances you could have a user pressing both right and up keys together and want it to mean something.

 

Link to comment
Share on other sites

Thanks for the help. That was similar to what I had already tried, or at least it sounded like it. I gave it a try anyway but wasn't able to get it working. In the end I found I can set the spinRate to negative and the spin direction will change. I didn't expect it to be that simple.

if (held & CONTROL_ROT_LEFT)        {                spinRate -= 2;                //llOwnerSay((string)spinRate);                spinLimit();        }                else if (held & CONTROL_ROT_RIGHT)        {                spinRate += 2;                //llOwnerSay((string)spinRate);                spinLimit();        }llTargetOmega(<0,0,1>, PI/180*spinRate, spinRate);   

Thanks!

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...