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:
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.
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?
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
Follow this Question
Related Questions
InvalidOperationException thrown 0 Answers
How do I make a script wait X time? 1 Answer
Playerprefs are not deleting when an apk is uninstalled? 0 Answers