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 Velocity · May 07, 2013 at 07:15 PM · movementvariablesstatic variable

Can you change Static Variable values from another script?

So whenever I press Shift, the Sprint Input Button, the animation plays, yet the WalkAcceleration stays the same. I'm pretty sure I'm missing something or maybe a line is overlapping another line.

Animation Script:

 var AnimController : GameObject;
 var rSound: AudioSource;
 var rLoading: boolean = false;
 var rLoadingAnim: Animation;
 var Sprinting : Animation;
 
 function Start() {
     // Set all animations to loop
    AnimController.animation.wrapMode = WrapMode.Loop;
    // except shooting
    AnimController.animation["Reload"].wrapMode = WrapMode.Once;
  
    // Put idle and walk into lower layers (The default layer is always 0)
    // This will do two things
    // - Since shoot and idle/walk are in different layers they will not affect
    //   each other's playback when calling CrossFade.
    // - Since shoot is in a higher layer, the animation will replace idle/walk
    //   animations when faded in.
    AnimController.animation["Reload"].layer = 1;
    // Stop animations that are already playing
    //(In case user forgot to disable play automatically)
    AnimController.animation.Stop();
  
 }
  
 function Update() {
     if (Input.GetButton("Sprint") && Input.GetButton("Vertical")) {
     animation.CrossFade("Run", 0.4);
     PlayerMovement.walkAcceleration = 11005;
     }
     else {
     animation.CrossFade("Idle", 0.5);
     PlayerMovement.walkAcceleration = 5000;
     }
     if (Input.GetKey("w") && !Sprinting.IsPlaying("Run")) {
     animation.CrossFade("Walk", 0.2);
     }
     if (Input.GetKey("a") && !Sprinting.IsPlaying("Run")) {
     animation.CrossFade("LeftWalk", 0.2);
     }
     if (Input.GetKey("d") && !Sprinting.IsPlaying("Run")) {
     animation.CrossFade("RightWalk", 0.2);
     }
     if (Input.GetKey("s") && !Sprinting.IsPlaying("Run")) {
     animation.CrossFade("Backwards", 0.2);
     }
     if (Input.GetButtonDown("Reload1") && !rLoading && GunScript.currentAmmo < 36) {
     animation.CrossFade("Reload", 0.2);
     rSound.Play();
     GunScript.currentAmmo = GunScript.maxAmmo;
     rLoading = true;
     }
     if (rLoading && !rLoadingAnim.IsPlaying("Reload"))
     {
     rLoading = false;
     }
 };

PlayerMovement Script:

 static var walkAcceleration: float = 5000;
 var walkAccelAirRatio: float = 0.1;
 var walkDeacceleration: float = 5;
 @HideInInspector
 var walkDeaccelerationVolx: float;
 @HideInInspector
 var walkDeaccelerationVolz: float;
 var cameraObject: GameObject;
 var maxWalkSpeed: float = 10;
 @HideInInspector
 var horizontalMovement: Vector2;
 var jumpVelocity: float = 20;
 @HideInInspector
 var grounded: boolean = false;
 var maxSlope: float = 60;
 
 @HideInInspector
 var paused : boolean = false;
 
 function Update () {
 
 transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
 if (horizontalMovement.magnitude > maxWalkSpeed)
     {
     horizontalMovement = horizontalMovement.normalized;
     horizontalMovement *= maxWalkSpeed;
     rigidbody.velocity.x = horizontalMovement.x;
     rigidbody.velocity.z = horizontalMovement.y;
     }
     
     if(grounded)
     {
         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
     }
     
         if (grounded)
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
         else
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
 
 horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
 
 
 
     if(Input.GetButtonDown("Jump") && grounded)
     {
         rigidbody.AddForce(0,jumpVelocity,0);
     }
 
 }
 
 function OnCollisionStay (collision:Collision) {
 
 for(var contact: ContactPoint in collision.contacts) {
     if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope) {
         grounded = true;
     }
 }
 
 }
 
 function OnCollisionExit () {
 grounded = false;
 }
Comment
Add comment · Show 1
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 ThunderEyed · May 07, 2013 at 08:16 PM 1
Share

Have you tried to put a Debug.log(walkAccelleration) to see if the problem is related to the Inputs? $$anonymous$$aybe the value gets modified and then resetted to 5000 and the animation seems to play due to the crossfade time. Sorry for my english.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eugenius · May 08, 2013 at 12:26 AM

I think that instead of making that a static variable and attempting to change it, you should use the GetComponent class to access the script and modify the variable as a public one. That worked for me more than once. Hope this helps!

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 Velocity · Jun 10, 2013 at 05:04 PM 0
Share

I haven't been working on my project for some time due to school, but I'll try these solutions. :D Any other solutions though will be most appreciated.

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

14 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

Related Questions

Adding variables together. 2 Answers

Access a variable from another script 1 Answer

Scripts/buttons getting old (prefab?) values of variables 1 Answer

How to modify the base movement speed of the character in JS 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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