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 Headstone7 · Jan 10, 2014 at 11:59 AM · characterscoreyaxis

How to "Increase score with Y axis"?

Hi, i would like to know how can i Jscript this: If the player goes up following the y axis, the score increases. If the player falls down, or goes down following the y axis, the score does not decrease.

This may seem obvious to you, but not to me unluckily :( Everything i looked for on the internet seemed unadapt for me and so it looks like my only chance is asking here ^^

Thanks for your help :3

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
2
Best Answer

Answer by Mikea15 · Jan 10, 2014 at 12:39 PM

You could go for something like this.

if your player's position is higher than your players last position increment score. or if the difference from your previous height and your current height is greater than 0, that means you've gone up. if its lesser than 0, you're falling down. also, you'd have to save your last position in the end.

 using UnityEngine;
 using System.Collections;
 
 public class JumpIncrementScore : MonoBehaviour 
 {
     [SerializeField]
     int score = 0;
 
     [SerializeField]
     Vector3 _lastPosition;
 
     // Use this for initialization
     void Start () {
         _lastPosition = this.transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         if( ( this.transform.position.y - _lastPosition.y ) > 0 )
             score++;
 
         _lastPosition = this.transform.position;
     }
 
     void OnGUI( ) {
         GUILayout.Label("Score : " + score);
     }
 }
Comment
Add comment · Show 9 · 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 Headstone7 · Jan 10, 2014 at 01:09 PM 0
Share

It gives me an error i can't eli$$anonymous$$ate on: Vector3 _lastPosition; telling me that a namespace can't contain things like methods, or something similar (had to translate from italian). Any idea?

avatar image Mikea15 · Jan 10, 2014 at 01:14 PM 0
Share

I just updated the code. I was forgoting to update the position. Are you using Javascript? This is c# code, and obviously you'll have to put this inside a Class. I haven't tested this, but will do. Let me know if you can't get there

avatar image Mikea15 · Jan 10, 2014 at 01:22 PM 0
Share

Hey, Headstone7, I've just updated the code above and tested it and it works fine. When you go up, it will increase the variable score. Just drag this c# script into a gameobject, hit play, and raise the object on the Y coordinate and you'll see the score incrementing. ;)

Cheers

avatar image Mikea15 · Jan 10, 2014 at 01:28 PM 0
Share

Yes, Player would be a class where you would expose a static Score variable. Assu$$anonymous$$g you'd have that. :) Just use the updated code above. ;) it should work fine.

avatar image Headstone7 · Jan 10, 2014 at 01:35 PM 0
Share

That's fantastic man! Thank you! Unluckily this also increases score with x modifications for some reason, i'll try to figure out how to remove this issue, let me know if you can imagine some solution ^^ Thanks again!

Show more comments

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

20 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

Related Questions

On Collision with water buoyancy and play Swim animation 1 Answer

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

Score display error 1 Answer

Accessing Script From Other Script Causes Lag? 1 Answer

How would I make the text bigger on this script? 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