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 /
  • Help Room /
avatar image
0
Question by UrsaGamer · Dec 05, 2017 at 11:59 AM · unity 5scripting problem

Boosting player speed permanently based on checkpoint position

Hi. I'm a newbie to Unity and C#, and am trying to make my first game level. It a simple game, where a player jumps over obstacles, much like a horse-riding obstacle course. The forward movement of the player is automatic, and isn't controlled by a keystroke. At certain checkpoints in the level, the forward speed is supposed to boost permanently, making the game more difficult to play.

I need help in coding this speed boost event. I tried using 'other.attachedrigidbody.AddForce' on the checkpoint trigger, but its effect decays over time, it is not permanent.

Now, I am trying a new bit of script, but its not working and its not returning an error message. Perhaps I'm writing the syntax correctly, but using it at the wrong place? I would appreciate it if an experienced pair of eyes can figure out how to script this event correctly. Please refer to the image below:

alt text

2017-12-05-17-16-39-2.png (50.4 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DarkC0der · Dec 05, 2017 at 09:31 PM

I think the best way to approach this is to make another class called CheckPoint which will store the speed multiplier and will wait for OnTriggerEnter function to fire (You will need to attach a collider of course). Then if Player enters the CheckPoints collider than you grab collider information from the function and check if it is player if so than grab its PlayerMovement script attached to it and multiply it with checkpoints speed multiplier. Then you won't need to hardcode values in your playerMovements fixed update method.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckPoint : MonoBehaviour
 {
     public float speedMultiplier = 1.5f; // For example by 50% speed increase
 
     bool HasBoosted = false; // Bool to check if already boosted, avoiding double boost if stays too long in the trigger
 
     void OnTriggerEnter(Collider obj) 
     {
         if(obj.collider.tag == "Player" && !HasBoosted)
         {
             PlayerMovement movement = obj.collider.GetComponent<PlayerMovement>();
 
             movement.moveSpeed *= speedMultiplier;
 
             HasBoosted = true;
         }
     }
 }
 

And also I would recommend you to use RigidBody.MovePosition instead of translate because translate doesn't care about physics calculations.

Comment
Add comment · 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
0

Answer by UrsaGamer · Dec 06, 2017 at 04:43 PM

Hi, Thanks a lot for your reply and the script that you prepared for this solution.I have created a separate class for the checkpoint trigger, and applied the script, creating the reference to 'Player.Movement', as you've shown, but it doesn't seem to work. I'm trying to replace the transform.translate with Rigidbody.MovePosition, but there is some error in the syntax, and I can't find the solution in the API. Could you please take a look at the script and tell me what the correct way of writing this syntax is?

alt text


2017-12-06-22-06-59.png (12.4 kB)
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 DarkC0der · Dec 07, 2017 at 05:39 AM 0
Share

You have to use your objects rigid body instance not the $$anonymous$$ain class for $$anonymous$$ovePosition, and also the move position moves the object to the given position as you can see in the parameter it needs position so you need to pass to it your players transform.position + your speed vector

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

236 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 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

InvalidOperationException thrown 0 Answers

How do i change this line so that it works as im getting the error Assets/BoxLauncher.cs(28,53): error CS0246: The type or namespace name `ScoreManager' could not be found. Are you missing a using directive or an assembly reference? 1 Answer

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

How do I make a script wait X time? 1 Answer

Playerprefs are not deleting when an apk is uninstalled? 0 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