Help a tired man with #C..... (Record Player in Horror Game)
So i have easy questions, but i am bad coding and have been working for hours without solving anything:(, this is a #C code. I have this script for an old Record Player in my horror game what it does is that it moves some parts ot it. (its an animation of the Record Player's arm and the disc rolling) That's not the problem, the script as it is works perfectly and plays the animation. I can choose from unity if i want the animation to play or not just by clicking on the box "Record Player Active" I simply want to add two things to this script. That this "animation" gets activated when the player is in the trigger of the object and presses "E" and stops playing when pressing "E" again (always beeing in the trigger) And the secod thing, that it plays music, i have the track but i dont know how to make it work:( If someone could send me the code with the solution it will help me a lot, i have been too long trying and always getting errors, im bad at #C This is the code: using UnityEngine; using System.Collections;
public class RecordPlayer : MonoBehaviour {
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
public bool recordPlayerActive = false;
GameObject disc;
GameObject arm;
int mode;
float armAngle;
float discAngle;
float discSpeed;
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
void Awake()
{
disc = gameObject.transform.Find("teller").gameObject;
arm = gameObject.transform.Find("arm").gameObject;
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
void Start()
{
mode = 0;
armAngle = 0.0f;
discAngle = 0.0f;
discSpeed = 0.0f;
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
void Update()
{
//-- Mode 0: player off
if(mode == 0)
{
if(recordPlayerActive == true)
mode = 1;
}
//-- Mode 1: activation
else if(mode == 1)
{
if(recordPlayerActive == true)
{
armAngle += Time.deltaTime * 30.0f;
if(armAngle >= 30.0f)
{
armAngle = 30.0f;
mode = 2;
}
discAngle += Time.deltaTime * discSpeed;
discSpeed += Time.deltaTime * 80.0f;
}
else
mode = 3;
}
//-- Mode 2: running
else if(mode == 2)
{
if(recordPlayerActive == true)
discAngle += Time.deltaTime * discSpeed;
else
mode = 3;
}
//-- Mode 3: stopping
else
{
if(recordPlayerActive == false)
{
armAngle -= Time.deltaTime * 30.0f;
if(armAngle <= 0.0f)
armAngle = 0.0f;
discAngle += Time.deltaTime * discSpeed;
discSpeed -= Time.deltaTime * 80.0f;
if(discSpeed <= 0.0f)
discSpeed = 0.0f;
if((discSpeed == 0.0f) && (armAngle == 0.0f))
mode = 0;
}
else
mode = 1;
}
//-- update objects
arm.transform.localEulerAngles = new Vector3(0.0f, armAngle, 0.0f);
disc.transform.localEulerAngles = new Vector3(0.0f, discAngle, 0.0f);
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
}
Answer by _Game_Dev_Dude_ · Nov 18, 2015 at 09:58 AM
I would not add them to this script I would create a new script. This new script would have to do 2 things:
It would have to be notified whenever a player enters the trigger you want.
it would then check in its update function if the player presses E.
Once both of these conditions are met, this script would toggle the recordPlayerActive bool in your current script and that should get it animating based on input.
As for the audio, just add some audio functionality to your record player code for when it is in the "Running" state and then ad Stop when it is in the Stopping phase.
http://docs.unity3d.com/ScriptReference/AudioSource.Play.html http://docs.unity3d.com/ScriptReference/AudioSource.Stop.html
Your answer
Follow this Question
Related Questions
I need help writing a script that triggers a character animation when entering a collider 0 Answers
Moving an Object to a random position 3 Answers
In my script the Animation selection pop up shows no assets even though i have several? 0 Answers
assign a new value to a variable 0 Answers
Move to next animation with clicking the game object 0 Answers