Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TheYoyii · Aug 18, 2013 at 07:16 PM · audiosourcerunbehaviour

Can't add script behaviour Run.

Hi ! I found a script to make my FPS run for a limited time. The script is correct but i dont understand why i can't put it on my First Person Player. This is the script.


 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(AudioSource))]
  
 public class RunAndCrouch : MonoBehaviour 
 {
     public float walkSpeed = 7; // regular speed
     public float crchSpeed = 3; // crouching speed
     public float runSpeed = 20; // run speed
     public float runDuration = 5.0f;
     public float runRegenTime = 15.0f;
 
     private CharacterMotor chMotor;
     private Transform tr;
     private float dist; // distance to ground
  
     bool canRun = true;
     bool tryRun = false;
  
     public AudioClip outOfBreath;
  
     // Use this for initialization
     void Start () 
     {
        ///AUDIO
        if(!GetComponent<AudioSource>())
          gameObject.AddComponent<AudioSource>();
        audio.clip = (AudioClip)outOfBreath;
        audio.loop = false;
        audio.playOnAwake = false;
        ///
         StartCoroutine("CheckRunEnergy");
         chMotor =  GetComponent<CharacterMotor>();
         tr = transform;
         CharacterController ch = GetComponent<CharacterController>();
         dist = ch.height/2; // calculate distance to ground
     }
  
     // Update is called once per frame
     void FixedUpdate ()
     {
        print (canRun);
        float vScale = 1.0f;
         float speed = walkSpeed;
  
         if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
         {
          tryRun = true;
          if(canRun)
                 speed = runSpeed;
          else if(!canRun)
           speed = walkSpeed;
         }
        else
          tryRun = false;
  
         if (Input.GetKey("c"))
         { // press C to crouch
             vScale = 0.5f;
             speed = crchSpeed; // slow down when crouching
         }
  
         chMotor.movement.maxForwardSpeed = speed; // set max speed
         float ultScale = tr.localScale.y; // crouch/stand up smoothly 
  
        Vector3 tmpScale = tr.localScale;
        Vector3 tmpPosition = tr.position;
  
        tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
        tr.localScale = tmpScale;
  
        tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position       
        tr.position = tmpPosition;
     }
  
     IEnumerator CheckRunEnergy()
     {
        while(true)
        {
          if(tryRun)
          {
           while(canRun)
           {
               yield return new WaitForSeconds(runDuration);
               canRun = false;
            audio.PlayOneShot(outOfBreath, 1.0f);
           }
           while(!canRun)
           {
               yield return new WaitForSeconds(runRegenTime);
               canRun = true;
           }
          }
  
  
          yield return new WaitForEndOfFrame();
        }
     }
 }


Im new on unity and sorry for my bad english.

Comment
Add comment · Show 4
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 Sajidfarooq · Aug 18, 2013 at 07:17 PM 0
Share

What is the error message?

avatar image TheSamari3397 · Aug 18, 2013 at 08:19 PM 0
Share

Does it just not let you add it to the GameObject?

avatar image TheYoyii · Aug 18, 2013 at 08:54 PM 0
Share

This is the message that popup when i try :

Can't add the script Run. The scripts file name does not match the name of the class defined in the script!

and yes he dont let me put the script on my First Person Player.

avatar image Sajidfarooq · Aug 18, 2013 at 09:15 PM 0
Share

That's precisely the error I was expecting. The answer is provided by @DaveA.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Aug 18, 2013 at 08:56 PM

Rename the file to RunAndCrouch.cs

Comment
Add comment · Show 1 · 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 TheYoyii · Aug 19, 2013 at 03:27 AM 0
Share

Work great :) thanks you

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

18 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

Related Questions

Multiple Cars not working 1 Answer

How to make my first player controller collidable? 1 Answer

What is the script to access my animation called "run" in my animator controller 1 Answer

How to Stop running after a few Seconds ? 1 Answer

Method keeps looping when using waitForSeconds 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