• 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 youngapprentice · Aug 10, 2014 at 08:17 PM · updatedatanormal

Get Last Value

Hey, all!

I need to tell if my character is moving uphill or downhill and change the speed accordingly. However, when I debug my values, the normal of the last measurement is ALWAYS identical to the normal of the current one. Is there something wrong with my code?

Thanks!- YA

     void Align( Transform player, RaycastHit2D hit ){
         float groundAngle = Vector3.Angle ( Vector3.up, hit.normal);
         Debug.Log ( "Last angle was: " + lastAngle );
         Debug.Log ("This angle is: " + groundAngle );
         if( groundAngle < angleLimit ){
             //Align player to surface norma
             player.transform.rotation = Quaternion.FromToRotation ( Vector3.up, hit.normal );
 
             float newSpeed = moveSpeed + ((groundAngle/angleLimit)*Mathf.Sign( groundAngle*lastAngle ))*speedFlux;
             transform.Translate( newSpeed*Time.deltaTime*Input.GetAxisRaw ("Horizontal"), 0, 0);
 
             //Move player
             Vector3 newPos = new Vector3( transform.position.x, hit.point.y-0.1f, transform.position.z);
             //Stick to face on y axis
             transform.position = newPos;
             lastAngle = groundAngle;
             Debug.Log ( "New speed is: " + newSpeed );
         }
         else Debug.Log("POOP");
         }
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 christoph_r · Aug 10, 2014 at 11:05 PM 0
Share

So if the value changes in each frame, does it still turn out to be the same for the last and the current angle? If so, there must be something else going on in another part of that script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Aug 11, 2014 at 12:01 AM

You might want to move lastAngle = groundAngle; to after the if statement.

Once lastAngle and groundAngle are the same (and this will happen if there is no movement between frames) lastAngle will never change.

Another option is to consider coroutines and having a several frame delay between checks.

Comment
Add comment · Show 2 · 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 youngapprentice · Aug 11, 2014 at 12:41 PM 0
Share

Okay. I did that and it didn't really work out. $$anonymous$$ight have to use coroutines. I am very surprised about this. I collect normal data before the move, during the move, and after the move, and the normals always match $$anonymous$$

avatar image youngapprentice · Aug 11, 2014 at 12:55 PM 0
Share

I might be able to solve this by casting 3 separate rays simultaneously and averaging them out to get the grade of the ground...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

problem updating info to data base 0 Answers

How to increase the speed of the character in OnTriggerEnter function? 1 Answer

Jumping randomly doesn't work 2 Answers

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

2d Character movement script (unity 4,3) 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