I'm kinda stuck, its seems very simple and I always get caught on simple stuff when its late but i need to finish this on a deadline so I'm asking for help.. What i want to do is slow down the vertical movement so the up and down motion isn't so fast on the object. Any suggestions on how I could do this.. here is the simple script i wrote Code (csharp): using UnityEngine; using System.Collections; public class BoxHit : MonoBehaviour { public PackedSprite boxSprite; private Vector3 box; private bool isHit = false; // Use this for initialization void Start () { box = transform.position; } // Update is called once per frame void Update () { transform.position = box; if(isHit){this.collider.isTrigger = false;} } void OnTriggerEnter(Collider other){ box.y += 0.3f; Debug.Log("Hit"); } void OnTriggerExit(Collider other){ box.y -= 0.3f; isHit = true; } }
Hi, Firstly don't write your script without Time.deltaTime. It helps you to cover Frame Rate Independent Game. Below is just an Idea of working this. Make modification as you required. Code (csharp): using UnityEngine; using System.Collections; public class BoxHit : MonoBehaviour { public PackedSprite boxSprite; public float currBoxSpeed = 30.0f; public float normalBoxSpeed = 30.0f; private float minBoxSpeed = 15.0f; private bool isHit = false; // Use this for initialization void Start () { currBoxSpeed = normalBoxSpeed ; } // Update is called once per frame void Update () { transform.Translate(Vector3.Up * boxSpeed * Time.deltaTime) } void OnTriggerEnter(Collider other){ currBoxSpeed = minBoxSpeed ; //slow Down here. } void OnTriggerExit(Collider other){ currBoxSpeed = maxBoxSpeed ; } }
Whats this? I know all about Time.deltaTime. this is not what I was looking for. I'm going to research a new way to achieve what I want in the morning thanks anyways
Looks to me like he gave you exactly what you were after, which makes your response more than a little rude. Best of luck...
kingcharizard, I wrote what you want. Game Object Speed depends on Box Speed, change it to make it slow or fast. For Up Down, i just added code for UP, just make it for DOWN by yourself.
How are you gonna tell me what works for my game? This is not my first project or my first script i'm not a beginner. That response wasn't rude it was strait to the point, he posted a script I didn't need so I told him so... you posting nothing relevant to the topic is rude.. your code moves a object up at a certain speed, what my code does is moves a certain object up on the y axis a fixed amount and then moves it back down, I'll explain. When the player collides with the boxObject the object moves up on the y axis Code (csharp): void OnTriggerEnter(Collider other){ box.y += 0.3f; Debug.Log("Hit"); } then when the player leaves the collider i move the box back into place using this code Code (csharp): void OnTriggerEnter(Collider other){ box.y -= 0.3f; isHit = true; } I then updated the movement every frame with this code Code (csharp): // Update is called once per frame void Update () { transform.position = box; if(isHit){this.collider.isTrigger = false;} } this works fine because it doesn't move the box unless im in contact with the collider. Whats its doing is moving up really fast and back down in place really fact which isn't what I want. I want the effect to be a bit slower. I don't want the box to move up at a certain speed. I need it to move up and back into place at a certain speed. I've tried to adjust the speed using variables and Time.deltaTime but when I put code like this in update Code (csharp): transform.position = (box * Time.deltaTime); it just raises the box even when im not in the trigger. when i try to add speed to the box.y variable is adds to the effect making it bigger or smaller so thats a no go either.. like I said I'm going to figure out a better way to achieve my desired result...
As you wrote that you are not a beginner. Then what does it mean to write: transform.position = box; And Regarding my code lines transform.position = (box * Time.deltaTime); .. You should add these lines in a flag Check(enable on Trigger Enter with Player Disable on Trigger Exit) If you want to move box by a particular unit(lets say 3units Up), in this case you should use a basic concept of store initial position to a initPosition, in Update() make a difference check whether you difference is greater than maxVar(Which is 3Units) perform operations on this basis.
Code (csharp): transform.position = box; this takes the box's updated position and puts it in transform.position as you should know the value on the right gets added or becomes the value on the left. I hard coded everything till i get it right. I don't use the inspector to change my values and since the script wasn't long i didn't mind changing both values. this code Code (csharp): transform.position = (box * Time.deltaTime); was my idea i used it to try to slow down the effect but it just incremented it constantly. I've tried many different ways before asking for help if this was simple i should have figured it out I dont understand what your getting at here but from reading that again I dont think you understand what I'm trying to achieve so here is a webplayer demo hit the question mark box http://dl.dropbox.com/u/36346467/WebPlayer.html see how fast the box goes up and down. I want that effect to slow down
You could try to modify the time of the effect, to give it a slowmo effect. http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html EDIT : You might also try to animate the box and by that you will have a effect as smooth as you want and also a shorter script. if (hit played = false) { anim play; played = true }
The web player isn't working for me, but I think I understand what you're after. You basically want an automatic opening and closing garage door. Is there a reason why you can't do this this with a Unity Animation? It seems simple to me. But it is also easy enough to code, lots of ways you can do it. Here's a basic state machine approach... Code (csharp): bool hitEnter = false; bool hitExit = false; bool liftUp = false; bool liftDown = false; float liftDuration = 0.5f; // seconds Vector3 boxLift = new Vector3(0, -0.3f. 0); Vector3 boxStart; float liftStartTime; void Start () { boxStart= transform.position; } void Update () { if (hitEnter) { liftStartTime = Time.time; liftUp = true; hitEnter = false; } if (liftUp) { float dt = (Time.time - liftStartTime) / liftDuration; if (dt >= 1) { dt = 1; liftUp = false; liftDown = true; liftStartTime = Time.time; } transform.position = Vector3.Lerp(boxStart, boxStart+boxLift, dt); } if (liftDown) { float dt = (Time.time - liftStartTime) / liftDuration; if (dt >= 1) { dt = 1; liftDown = false; } transform.position = Vector3.Lerp(boxStart+boxLift, boxStart, dt); } if (hitExit) { // reset state liftUp = false; liftDown = false; hitExit = false; transform.position = boxStart; } } void OnTriggerEnter(Collider other) { hitEnter = true; } void OnTriggerExit(Collider other) { hitExit = true; } This code is untested. I should also mention that this program assumes the actor is passing through the collision volume and takes enough time to allow the door, or whatever it is, to lift up and back down. This is an assumption on my part. You haven't explained your application in enough detail to know exactly what you want to do. And, unfortunately, I still can't get the web player to play nice in Chrome, so I can't look at your project.... I don't think this is your fault though, I think something is broken on my end.
You asked a question and got an answer. It wasn't the answer you were looking for, and where you _might_ have considered thanking them for their time, effort, input... and then explained that you were looking for a different solution... you instead responded like a spoiled child. The fact that you can't see how incredibly rude your reply was simply tells me you don't even realize when you're being rude. Whether this is your first rodeo or not, I suggest you get your Pikachu underoos unknotted. Best of luck with that and your code. 'Nuff said.
please stay out of my topic, but if you intend and insist on wasting my time then tell me how i was rude? I told them directly their code"effort" didn't get what i wanted and proceeded to tell them i would solve the problem on my own. Spoiled child? really? far from it infact i am poor, and im proud of where I come from because when i succeed It will be because I worked my ass off to achieve what i have. which is the case now. But if you insist i was being rude please tell me how should have answered the reply...
ohoho Easy here ultrafree people of USA. Krougeu you are more likely a spoiled child, you must explain him what happens around here instead of starting to swear because you think of yourself as an "grown-up"
oh btw everything you said i should have done I'm pretty sure I actually did End of discussion! if I do not get the desired result that i want i will post again..
I'm no expert, but what I would do to slow down time I just do this : Code (csharp): "Thing to slow down" = Time.deltaTime / "amount I want to slow down";
try Time.deltaTime http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html function Update () { // Move the object 10 meters per second! var translation : float = Time.deltaTime * 10; transform.Translate (0, 0, translation); }
I tried that it didn't quite work right. I already got it thanks anyways. Code (csharp): "Thing to slow down" = Time.deltaTime / "amount I want to slow down"; I've tried that it either adds/subtracts to the effect deppending on the result of the math.. I figured it out. I no longer need help, but thanks anyways
Hi Guys, I think this is a case of Misunderstanding each other And @kingcharizard I think this is what you want. Code (csharp): using UnityEngine; using System.Collections; public class BoxHit : MonoBehaviour { //public PackedSprite boxSprite; private Vector3 box; private bool isHit = false; private bool SlowDown = false; // Use this for initialization void Start () { box = transform.position; } // Update is called once per frame void Update () { print(SlowDown); transform.position = box; if(isHit){this.collider.isTrigger = false;} if(SlowDown){StartCoroutine(Wait());SlowDown = false;} } void OnTriggerEnter(Collider other){ box.y += 0.3f; Debug.Log("Hit"); } void OnTriggerExit(Collider other){ SlowDown = true; } IEnumerator Wait() { yield return new WaitForSeconds(1); box.y -= 0.3f; isHit = true; } } I tried the code its working.