Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GorGargoullieth10th8 · Apr 23, 2019 at 03:27 PM · vector3axisangles1st person

three second timer.

Hi everyone, I am creating a 3d flying camera game, where the camera flies around on all three axises, and I am trying to figure out how to get my "Overdrive" feature, that goes at 100f, to work Here's my current code. I don't currently have a timer feature set yet so yeah. using System.Collections; using System.Collections.Generic; using UnityEngine;

public class move : MonoBehaviour { private bool stop = false; public float smooth = 5.0f; public float tiltAngle = 60f; public float moveSpeed = 2f;

 void Update()
 {
     if (stop == false)
     {
         Vector3 movement = transform.rotation * Vector3.forward * moveSpeed * Time.deltaTime;
         transform.position += movement;
     }

     float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
     float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;

     Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0);
     transform.rotation *= target;

     if (Input.GetKeyDown(KeyCode.P))
     {
         stop = !stop;
     }
     if (Input.GetKeyDown(KeyCode.Space) && moveSpeed == 2f)
     {
         moveSpeed = 20f;
     }
     else if (Input.GetKeyDown(KeyCode.Space) && moveSpeed == 20f)
     {
         moveSpeed = 2f;
     }
     if (Input.GetKeyDown(KeyCode.O) && Input.GetKeyDown(KeyCode.Z) && moveSpeed == 20f)
     {
         Debug.Log ("overdrive enabled")
          moveSpeed = 100f;
     }
     else if (Input.GetKeyDown(KeyCode.O) && Input.GetKeyDown(KeyCode.Z) && moveSpeed == 100f)
     {
         Debug.Log ("overdrive disabled")
         moveSpeed = 2f;
     }
 }

}

What am I doing wrong? Is there something that I missed when adding this? Also how to attach a timer to it is necesarry.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by K-Anator · May 06, 2019 at 08:51 PM

If I've learnt one thing in the past month of working in Unity, it's that co-routines are good for timers. I don't fully understand them per say, but I've managed to do a few things with various iterations of this script:

     [SerializeField]
     private float startTimer;
     private GameObject player;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         StartCoroutine("CountDown");
     }
 
     IEnumerator CountDown()
     {
         yield return new WaitForSeconds(startTimer);
         player.GetComponent<MovementController>().AllowMovement();
     }


something like this should be close to what you need:

     [SerializeField] private float OverdriveTime;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Z))
         StartCoroutine("OverDrive"); //Start dat co-routine
     }
 
     IEnumerator OverDrive()
     {
         moveSpeed = 200f; //Become a fast boi
         yield return new WaitForSeconds(OverdriveTime);  //Wait the amount of time you want
         moveSpeed = 2f; //Become a slow boi
     }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chaitae · Apr 26, 2019 at 10:35 PM 0
Share

Once you press space then simultaneously press O and Z overdrive is enabled but the code right after that immediately disables it because it is input.getkeydown() O and Z.

avatar image GorGargoullieth10th8 chaitae · May 04, 2019 at 11:09 PM 0
Share

I would agree with that if there wasn't the else statement in there. isn't that supposed to wait until you take your fingers off the keys to actually have that working? or how should I fix that?

avatar image GorGargoullieth10th8 chaitae · May 06, 2019 at 08:08 PM 0
Share

also, I just changed the code so that it is only the Z key. but all I really need is fro the timer to be attached.

avatar image GorGargoullieth10th8 chaitae · May 08, 2019 at 04:35 PM 0
Share

so now everything is working as it should but there are three errors. using System.Collections; using System.Collections.Generic; using UnityEngine;

public class move : $$anonymous$$onoBehaviour { private bool stop = false; public float smooth = 5.0f; public float tiltAngle = 30f; public float moveSpeed = 2f; public float OverdriveRun = 3f; private bool OverdriveRunning = false; private bool CanRun = true; public float Cooling = 15f; private bool OverdriveCooldown = false;

 void Start()
 {
     gameObject.GetComponent<ParticleSystem>().enableEmission = false;
 }
 void Update()
 {
     if (stop = false)
     {
         tiltAngle = 30f;
         Vector3 movement = transform.rotation * Vector3.forward * moveSpeed * Time.deltaTime;
         transform.position += movement;
     }
     
     else
     {
         tiltAngle = 10f;
     }
     
     float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
     float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;

     Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0);
     transform.rotation *= target;

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))
     {
         stop = !stop;
     }
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && moveSpeed < 20f)
     {
         Debug.Log ("faster");
             moveSpeed = 20f;
         
     }
     
     else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && moveSpeed >= 20f)
     {
         Debug.Log ("slower");
         moveSpeed = 2f;
     }
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.O) && moveSpeed >= 20f && moveSpeed < 100f && CanRun = true)
     {
         Debug.Log ("evenfaster");
            moveSpeed = 100f;    
            OverdriveRunning = true;
     } 
     
     if (OverdriveRunning = true)
     {
      OverdriveRun - Time.deltaTime;
     }
     
     if (OverdriveRun <= 0)
     {
         OverdriveRunning = false;
            moveSpeed = 2f;
            OverdriveCooldown = true;
     }
     
     if (OverdriveCooldown = true)
     {
         Cooling - Time.deltaTime;
     }
     
     if (Cooling <= 0)
     {
         CanRun = true;
         OverdriveRun = 3f;
         OverdriveCooldown = false;
     }
     
     if (OverdriveCooldown = false)
     {
         Cooling = 15f;
     }
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && moveSpeed >=2f)
     {
         moveSpeed = 30f;
         gameObject.GetComponent<ParticleSystem>().enableEmission = true;
     }
     
     else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && moveSpeed >= 30f && moveSpeed < 31f)
     {
         moveSpeed = 2;
         gameObject.GetComponent<ParticleSystem>().enableEmission = false;
     }
 }

}

avatar image GorGargoullieth10th8 GorGargoullieth10th8 · May 08, 2019 at 04:37 PM 0
Share

note: there is a particle system attached, and there is a cool down time attached as well as the three seconds of Overdrive.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Obtain angle from given rotation 1 Answer

Accouting for random rotations in Rotate Around 1 Answer

Vector3.SignedAngle changes sign for no reason 1 Answer

Find a Position on the Axis of a GameObject 1 Answer

Character Controller Movement - Different speeds on different axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges