Search Unity

Engine dependent on Rigidbody.Velocity and Desired Velocity

Discussion in 'Scripting' started by Firlefanz73, Jun 20, 2020.

  1. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello,

    my spaceship got 6 engines, two point forward, two backward and one left and right each.

    I want a throttle for each, so if the spaceship flies Forward, turn on the backward pointing engines etc.
    Code (CSharp):
    1.      
    2. Vector3 desiredVelocity = Input.GetAxis("Horizontal") * transform.right * 6f + Input.GetAxis("Vertical") * transform.forward * 4f;
    3. _rigidbody.velocity = Vector3.Lerp(_rigidbody.velocity, desiredVelocity, Time.deltaTime * Speed);
    4.  
    How do I get the throttle? I was trying with Vector3.Dot, but do not get the desired results:
    Code (CSharp):
    1.         foreach (JetParticleEffect engine in Engines)
    2.         {
    3.                UpdateEngines(_rigidbody.velocity, desiredVelocity);
    4.         }
    5.  
    6.         public void RefreshVelocity(Vector3 velocity, float dot)
    7.         {
    8.             ParticleSystem.MainModule mainModule = m_System.main;
    9.             // update the particle system based on the jets throttle
    10.             float throttle = 0;
    11.             if (dot > 0.5)
    12.             {
    13.                 throttle = velocity.magnitude;
    14.             }
    15.             mainModule.startLifetime = Mathf.Lerp(0.0f, m_OriginalLifetime, throttle);
    16.             mainModule.startSize = Mathf.Lerp(m_OriginalStartSize * .3f, m_OriginalStartSize, throttle);
    17.             mainModule.startColor = Color.Lerp(minColour, m_OriginalStartColor, throttle);
    18.         }
    19. [LEFT][FONT=Helvetica][COLOR=rgb(51, 51, 51)]
    Any idea how to get the engines emitting dependent of my flight direction?

    Parts of it where taken from standard assets jet.

    Thanks! :)
     
  2. I maybe under-thinking this and I missed some sleep last night, so I may be completely dumb today, but isn't it purely the deltaVelocity? If your velocity-change on the front-back line (the dot product...) is positive than you turn the back thruster, if it's negative then the front one?

    I'm thinking because of the inertia and all, you need to check the velocity-change and not the velocity itself.
     
    Firlefanz73 and PraetorBlue like this.
  3. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Thanks for the idea!
    I made a Enumeration for four Directions, a class EnginePart with GameObject and Directions, and just look if DesiredVelocity X or Y is <>0 and Setactive each one. Works very well.

    Solved :)
     
    Lurking-Ninja likes this.