- Home /
Trying to use variables from 'FirstPersonController' to slow down player inside a box collider. Please help!
Hey guys, first of all thanks for reading and maybe helping ;-)
Im working on a script that slows a player while inside a box collider that is a trigger.
It works by dividing the speed variables from the 'FirstPersonController' Standard Unity asset by the 'SpeedDivide' variable inside my c# script.
When the player is inside the water, the script should play an audio sound (watersound) and decrease the Playerspeed by 50% (dividing by 2).
the script goes as follows: (its in c#)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SlowDownWater : MonoBehaviour {
public float SpeedDivide = 2; bool InWater = false; public AudioClip watersound;
void Start()
{
GameObject MainCharacter = GameObject.Find("Player");
var playerScript = MainCharacter.GetComponent<FirstPersonController>();
}
void OnTriggerEnter()
{
InWater = true;
playerScript.m_WalkSpeed /= SpeedDivide;
playerScript.m_RunSpeed /= SpeedDivide;
playerScript.m_JumpSpeed /= SpeedDivide;
Debug.Log("LOG :MainCharacter in water");
}
void OnTriggerExit()
{
playerScript.m_WalkSpeed *= SpeedDivide;
playerScript.m_RunSpeed *= SpeedDivide;
playerScript.m_JumpSpeed *= SpeedDivide;
InWater = false;
Debug.Log("LOG: MainCharacter out of water");
}
void Update()
{
if(InWater == true)
{
audio.PlayOneShot(watersound);
Debug.Log("LOG : Sound has played!");
}
}
}
now ive got 7 errors that all say something like:
Assets/Scripts/SlowDownWater.cs(29,5): error CS0103: The name playerScript' does not exist in the current context and 1 that says : Assets/Scripts/SlowDownWater.cs(21,49): error CS0246: The type or namespace name
FirstPersonController' could not be found. Are you missing `UnityStandardAssets.Characters.FirstPerson' using directive?
I appreciate all help, keep in mind im still learning Unity and C# so there might be some basic bugs.
Answer by PixelSpartan · Feb 14, 2017 at 04:49 PM
When using GetComponent(), use UnityStandardAssets.Characters.FirstPerson.FirstPersonController and not just FirstPersonController
Your answer
Follow this Question
Related Questions
OnTriggerEnter function in c# 0 Answers
Make object react to certain triggers only 1 Answer
Slenderman like game pages not working! 2 Answers
Box collider with strange behaviour 1 Answer
OnTriggerEnter/Exit called unexpectedly 2 Answers