- Home /
How to limit the transform.Translate distance
Hey there folks,
I've created this simple code that is an activated animation to open/close a sliding door when it's been clicked (well, when the right click is being held down to close it, it also has a seperate script that is almost identical to hold down left click to open it).
I want to be able to limit the distance the door can close, naturally; how can I expand on this code to make it do so?
C#
using UnityEngine;
using System.Collections;
public class cabdoorclose : MonoBehaviour
{
public void OnDoorclose()
{
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
This is the clickable object to open/close the door:
C# using UnityEngine; using System.Collections;
public class cabdoorclick : MonoBehaviour
{
public cabdooropen dooropen;
public cabdoorclose doorclose;
void OnMouseEnter ()
{
guiText.text = "Hello World!";
}
void OnMouseOver ()
{
if (Input.GetMouseButton (1)) {
doorclose.OnDoorclose();
}
if (Input.GetMouseButton (0)) {
dooropen.OnDooropen();
}
}
}
Answer by robertbu · Feb 20, 2013 at 09:50 PM
Is this code working? The problem I see is that OnDoorclose() is only called once, but with a Time.deltaTime included, it should be called repeatedly from Update(). One way to move something over time is to use Vector3.MoveTowards():
Here is an example script. In the inspector set v3Open and v3Closed to the positions you want the door in when it is open/closed. Call SetOpenClose() to either open or close the door.
public class OpenClose : MonoBehaviour {
public Vector3 v3Open;
public Vector3 v3Close;
public float fDistPerSecond = 1.0f;
private Vector3 v3To;
void Start() {
v3To = transform.position;
}
void SetOpenClose(bool bOpen) {
if (bOpen)
v3To = v3Open;
else
v3To = v3Close;
}
void Update () {
transform.position = Vector3.MoveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
}
}
Hi Robert,
yes the code does work as it is, just moving infinitely if I held the mouse click on it for that long.
I've struggled to understand what you meant by "In the inspector set v3Open and v3Closed to the positions you want the door in when it is open/closed", forgive me but i've only been using Unity for 3/4 days.
Do you mean move it by hand and somehow save and label that state? if so, how do I go about this?
Thanks also for your reply, it is appreciated.
$$anonymous$$ind regards,
I recommend testing this code in a new project with just a camera and a cube at the origin. Attach the modified version of the script below to the cube. Click on the cube in the hierarchy panel. In the Inspector panel you will see the OpenClose script. Three variables will be listed v3Open, v3Close, and FDistPerSecond. FDistPerSecond sets how fast the object will move. v3Open and v3Close are the positions that the object considers open and closed. Click on the triangle next to v3Open and the x,y,z values will be displayed. You can change the values. For the test, use (-1,0,0) for v3Open and (1,0,0) for v3Closed. Run the script.
using UnityEngine;
using System.Collections;
public class OpenClose : $$anonymous$$onoBehaviour {
public Vector3 v3Open;
public Vector3 v3Close;
public float fDistPerSecond = 1.0f;
private Vector3 v3To;
void Start() {
v3To = transform.position;
InvokeRepeating("RepeatOpenClose",0.0f, 3.0f);
}
void SetOpenClose(bool bOpen) {
if (bOpen)
v3To = v3Open;
else
v3To = v3Close;
}
void Update () {
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
}
bool b = false;
void RepeatOpenClose() {
SetOpenClose (b);
b = !b;
}
}
The additions I made to this modified version trigger the open/close method very 3 seconds.
Hi Robert,
I've followed your instructions and edited it over many hours now- by default the door just flies off into the distance infinitely- i've tried lowering the value to 0.01 to see if it was just set too far- but to no avail unfortunately. Am I doing something wrong with simply applying your latest code to a cube and setting the values in the inspector to 1? Did you mean for this to be linked to one of my existing C# scripts?
The most I managed to develop your script into (something that the user could control by any input) was the following, which made the door fly infinitely until it was clicked- then it would stop and nothing further could be done with it.
using UnityEngine;
using System.Collections;
public class cabdooropenclose : $$anonymous$$onoBehaviour
{
public Vector3 v3Open;
public Vector3 v3Close;
public float fDistPerSecond = 0.1f;
private Vector3 v3To;
void On$$anonymous$$ouseDown ()
{
v3To = transform.position;
}
void SetOpenClose(bool bOpen) {
if (bOpen)
v3To = v3Open;
else
v3To = v3Close;
}
void Update () {
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
}
bool b = true;
void RepeatOpenClose() {
SetOpenClose (b);
b = !b;
}
}
Thanks for your responses so far; I hope to get there soon ;)
I made the change you were trying to make. If you click on it, the door will open/close. Before you integrate it into your scene: 1) Create a new scene, 2) Create a block at the origin (0,0,0), 3) Attach the script. Note you might get a fast initial move if you did not place your block at the origin to start.
using UnityEngine;
using System.Collections;
public class OpenClose2 : $$anonymous$$onoBehaviour
{
public Vector3 v3Open = new Vector3(1.0f, 0.0f, 0.0f);
public Vector3 v3Close = new Vector3(-1.0f, 0.0f, 0.0f);
public float fDistPerSecond = 1.0f;
bool b = true;
private Vector3 v3To;
void On$$anonymous$$ouseDown ()
{
SetOpenClose (b);
b = !b;
}
void SetOpenClose(bool bOpen) {
if (bOpen)
v3To = v3Open;
else
v3To = v3Close;
}
void Update () {
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
}
}
Thanks Robert! This is doing exactly what I expect it to! I am sure I will have no problem learning from it and adapting it to be within my scene!
$$anonymous$$indest regards,
Your answer
Follow this Question
Related Questions
Transform.Translate causing object to teleport 0 Answers
trigger animation problems 2 Answers
open a animated door 3 Answers
Opening a door when a button object has something it it's collider. 0 Answers