Hi, I’m pretty new to Unity so this may sound like a dumb question, but I’m stuck. Currently I am working on a simple block racing game, that uses a constant force to push the block forward. I want to create an area on the map which would give the player a speed boost while in the area (kinda like the arrows on the ground in Mario Kart). I figured I could do this by creating an empty game object, giving it a box collider, and writing OnTriggerEnter() script, so that when the player enters that zone, the force and therefore the speed of the player will increase. The problem isn’t that the player doesn’t speed up, it’s that the boost activates at the start of the level, not when the player collides with the game object that is the speed boost area. Here is the code I used (in C#).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class speedBoost1 : MonoBehaviour {
public GameObject Player;
private PlayerMovement otherScriptToAccess;
public float boostAmount = 2000f;
// Use this for initialization
void Start ()
{
}
private void OnTriggerEnter(Collider other)
{
//accesses the script that stores the forces acting on the player
otherScriptToAccess = Player.GetComponent<PlayerMovement>();
//accesses the variable in that script called forwardForce
otherScriptToAccess.forwardForce = otherScriptToAccess.forwardForce + boostAmount;
Debug.Log("BOOST!");
}
void OnTriggerExit(Collider other)
{
otherScriptToAccess.forwardForce = otherScriptToAccess.forwardForce - boostAmount;
Debug.Log("normal");
}
// Update is called once per frame
void Update () {
}
}
Any help would be much appreciated!