- Home /
How to give a timing to my script?
Hi all, i create this script to move my cute T-Rex. But i have problem, beacause i want to time (like 2 seconds of timer) the transition between camera position.
Here is the script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float PlayerWalkSpeed = 20.0f;
public float PlayerRotationSpeed = 80.0f;
public GameObject View;
Vector3 ViewOriginalPosition;
Vector3 ViewBobbingPosition;
private bool isWalking = false;
// Use this for initialization
void Start () {
ViewOriginalPosition = new Vector3(0.0f, 291.0f, -47.0f);
ViewBobbingPosition = new Vector3(0.0f, 311.0f, -47.0f);
}
// Update is called once per frame
void Update () {
if (Input.GetKey ("w")) {
transform.Translate ((Vector3.forward) * PlayerWalkSpeed * Time.deltaTime);
isWalking = true;
} else if (Input.GetKey ("s")) {
transform.Translate (-(Vector3.forward) * PlayerWalkSpeed * Time.deltaTime);
isWalking = true;
}
if (Input.GetKey ("a")) {
transform.Rotate (-(Vector3.up) * PlayerRotationSpeed * Time.deltaTime);
isWalking = true;
} else if (Input.GetKey ("d")) {
transform.Rotate (Vector3.up * PlayerRotationSpeed * Time.deltaTime);
isWalking = true;
}
if (isWalking == true) {
if(View.transform.localPosition == ViewOriginalPosition){
View.transform.localPosition = ViewBobbingPosition;
} else if (View.transform.localPosition == ViewBobbingPosition){
View.transform.localPosition = ViewOriginalPosition;
}
}
}
}
Someone can help me? :)
Comment
make a float called timer and give it a value of 2, in the update say timer -= Time.deltaTime;
Did it work? if yes please mark this as answered. if not ill figure it out now
Answer by mjood-10 · Jun 16, 2015 at 05:41 AM
var Player : Transform;
var CameraTime = 0.3;
private var mycamera : Transform;
private var cameratrans : Vector2;
function Start () {
mycamera = transform;
}
function Update () {
mycamera.position.x = Mathf.SmoothDamp(mycamera.position.x ,Player.position.x ,cameratrans.x ,CameraTime);
mycamera.position.y = Mathf.SmoothDamp(mycamera.position.y ,Player.position.y ,cameratrans.y ,CameraTime);
}
to have smooth move (camera with your cute T-Rex)
Thanks, but i need in C# and i don't understand this script in JAVA hahah
Your answer
