Question by
infocavasino · Jan 18, 2018 at 06:49 PM ·
playerfreeze
How do i freeze the player for the firs 3 second?
Hello guys, i'm new with this program and with the C#, i would like to know how can i freez the player for the first 3 second of the game?
This is the script that i used:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class PlayerMotion : MonoBehaviour {
private CharacterController controller;
private Vector3 moveVector;
private float speed = 6f;
private float verticalVelocity = 0f;
private float gravity = 12f;
private float animationDuration = 3.0f;
public float startTime;
public bool isDead = false;
//
//
void Start () {
controller = GetComponent <CharacterController> ();
startTime = Time.deltaTime;
}
void Update () {
if (isDead) {
return;
}
if (Time.time - startTime < animationDuration)
{
controller.Move (Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
//X
moveVector.x = Input.GetAxisRaw ("Horizontal") * speed;
if (Input.GetMouseButton (0))
{
if (Input.mousePosition.x > Screen.width / 2)
moveVector.x = speed;
else
moveVector.x = -speed;
}
//Y
moveVector.y = verticalVelocity;
//Z
moveVector.z = speed;
controller.Move (moveVector * Time.deltaTime);
}
public void SetSpeed(float modifier)
{
speed = 6f + modifier;
}
private void OnControllerColliderHit (ControllerColliderHit hit)
{
if (hit.collider.tag == "Obstacol")
Death();
}
public void Death ()
{
isDead = true;
GetComponent <Score> ().OnDeath ();
}
}
Thank you guys
Comment
Answer by R-Type · Jan 24, 2018 at 10:16 AM
You can do something pretty simple like this:
private float tActive;
public float tWait = 3.0f; // set this in inspector to any other value in seconds, if desired
void Start() {
tActive = 0.0f;
}
void Update() {
tActive += Time.deltaTime;
if (tActive < tWait) {
// do whatever your waiting action is
return;
}
// do your action here
}