Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Incremental control input

Discussion in 'Scripting' started by soundman, Oct 26, 2006.

  1. soundman

    soundman

    Joined:
    Oct 22, 2006
    Posts:
    6
    Hi there - I'm only just starting out facing a huge learning curve so please be gentle with me :wink:

    I've got a setup with throttle control on the w/s keys using rigidbody.AddRelativeForce to provide the motion. At the moment pressing 'w' gives forward motion pressing 's' gives rearward motion.

    What I would really like it to do is have a graduated throttle - say with values 1-10 that increase sustain. The longer a key is pressed, or the more times a key is tapped alters the throttle setting (with no reverse).

    Here's what I have at the moment:
    Code (csharp):
    1.  
    2. // check Throttle input
    3. if (Input.GetButton ("Throttle"))
    4. {
    5. flyForward = Input.GetAxis ("Throttle") * moveSpeed;
    6. flyForward *= Time.deltaTime; // adds consistency across varying framerates
    7. }
    8.  
    9. // apply Throttle
    10.     rigidbody.AddRelativeForce (-(flyForward), 0, 0);
    11.  
    Any clues or pointers would be greatly appreciated - unfortunately I'm sure there'll be more questions to come :roll:

    thanks

    Tom
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. function FixedUpdate ()
    3. {
    4.    flyForward = Input.GetAxis ("Throttle") * moveSpeed;  
    5.     rigidbody.AddRelativeForce (-(flyForward), 0, 0);
    6. }
    7.  
    Forces are already independent of framerate so no need to multiply by delta time.


    You can use input axes for the slow acceleration. Simply make the gravity sensitivity values smaller.
     
  3. soundman

    soundman

    Joined:
    Oct 22, 2006
    Posts:
    6
    Excellent - thanks Joachim, that's got it on the right track the sensitivity gravity settings were far too high for what I needed.

    Just have to figure out how to stop it going into reverse now :D
     
  4. soundman

    soundman

    Joined:
    Oct 22, 2006
    Posts:
    6
    I have another stumbling block now, well a couple really :roll:

    i have a plane flying around - controlled by analogue input. I have the controls being operated by forces being applied at the relative points on the airframe to make it move around....this is working fine.

    At the moment when the throttle is killed, the plane stops dead - how can I make it continue using momentum?
    This is the section of code I'm using at the moment
    Code (csharp):
    1. //***THROTTLE CONTROL***
    2. //Tells thottle amount that range -1 to 1 is actually 0 to 1
    3. flyForward = (((Input.GetAxis ("Throttle")/2)+ 0.5) * moveSpeed) ;
    4. // apply forward movement
    5. rigidbody.AddRelativeForce (-flyForward, 0, 0);
    6.  
    To follow on - I have control surfaces on the plane that I want to move proportionally with the analogue input (not continuous rotation)....what command can I use to make this happen....this is for visual benefit only.
    I'm thinking that it should be able to fit in with this kind of idea:
    Code (csharp):
    1. transform.Find("Unity plane 02 [Rudder]").rigidbody.AddRelativeForce(0, -rudderRotate, 0);
    where instead of adding a force, it applies a fixed rotation dependant on control input.

    any pointers will be greatfully appreciated