The question is answered, right answer was accepted
Why is transform.translate not working?
float drehen;
Vector3 Richtung = new Vector3(0f,0f,1f);
// Use this for initialization
void Start () {
drehen = 1;
Richtung = Vector3.left;
}
// Update is called once per frame
void Update () {
transform.Translate (0.1f*Vector3.left*Time.deltaTime);
}
hi there
This is the beginn of a script to controll the movement of my robot. But I have a problem. transform.Translate doesn't move it at all. What did i wrong? Could anyone help me please?
thanks
I changed my Version of unity from 5.4.0f3 to 5.3.2f1 and now this part is working. But as soon as i completed my script, i recognised, that the second part with the OnTriggerEnter is doing nothing. But i now that the scriptt is going throug on trigger because the System reports, if i write in a undefined tag. float drehen; Vector3 Richtung = new Vector3(0f,0f,1f); private Animator anim; // Use this for initialization void Start () { drehen = 1; Richtung = Vector3.forward; anim = GetComponent (); }
// Update is called once per frame
void Update () {
transform.Translate (2.8f*Richtung*Time.deltaTime, Space.World);
}
void OnTriggerEnter(Collider other){
if (other.CompareTag ("z+")) {
if (drehen == 2) {
drehen = 1;
Richtung = Vector3.forward;
anim.SetBool("right",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.forward, Space.World);
anim.SetBool ("right", false);
}
if (drehen == 4) {
drehen = 1;
Richtung = Vector3.forward;
anim.SetBool("left",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.forward, Space.World);
anim.SetBool("left",false);
}
}
if (other.CompareTag ("z-")) {
if (drehen == 2) {
drehen = 3;
Richtung = Vector3.forward;
anim.SetBool("left",true);;
StartCoroutine (delay ());
transform.Rotate (Vector3.back, Space.World);
anim.SetBool("left",false);
}
if (drehen == 4) {
drehen = 3;
Richtung = Vector3.forward;
anim.SetBool("right",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.back, Space.World);
anim.SetBool("right",false);
}
}
if (other.CompareTag ("x+")) {
if (drehen == 1) {
drehen = 2;
Richtung = Vector3.forward;
anim.SetBool("right",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.right, Space.World);
anim.SetBool("right",false);
}
if (drehen == 3) {
drehen = 2;
Richtung = Vector3.forward;
anim.SetBool("left",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.right, Space.World);
anim.SetBool("left",false);
}
}
if (other.CompareTag ("x-")) {
if (drehen == 1) {
drehen = 4;
Richtung = Vector3.forward;
anim.SetBool("left",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.left, Space.World);
anim.SetBool("left",false);
}
if (drehen == 3) {
drehen = 4;
Richtung = Vector3.forward;
anim.SetBool("right",true);
StartCoroutine (delay ());
transform.Rotate (Vector3.left, Space.World);
anim.SetBool("right",false);
}
}
}
IEnumerator delay(){
yield return new WaitForSeconds (5);
}
The "StartCoroutine (delay ());" doesn't actually delay the program flow. The code will continue immediately. You can only wait inside a CoRoutine. Otherwise you'd block the program.
Thank you I changed some things and now everything is just working fine except the rotation. It should rotate immediately with no interpolation, but at the moment he's jjust doing nothing. using UnityEngine; using System.Collections; [RequireComponent(typeof(CapsuleCollider))] public class behavior : $$anonymous$$onoBehaviour { float drehen; Vector3 Richtung = new Vector3(1f,0f,0f); private Animator anim; private Rigidbody body; public Quaternion rotieren=Quaternion.Euler(new Vector3(1f,0f,0f)); // Use this for initialization void Start () { drehen = 1; Richtung = Vector3.forward; anim = GetComponent (); body = GetComponent (); body.velocity =5*Richtung; rotieren=Quaternion.Euler(new Vector3(0f,1f,0f)); }
// Update is called once per frame
void FixedUpdate () {
body.$$anonymous$$oveRotation(rotieren);
}
void OnTriggerEnter(Collider other){
Debug.LogWarning("in OnTriggerenter Event");
if (other.CompareTag ("z+")) {
if (drehen == 2) {
drehen = 1;
Richtung = Vector3.forward;
anim.SetBool("right",true);
StartCoroutine (delay ());
}
if (drehen == 4) {
drehen = 1;
Richtung = Vector3.forward;
anim.SetBool("left",true);
StartCoroutine (ldelay ());
}
}
if (other.CompareTag ("z-")) {
if (drehen == 2) {
drehen = 3;
Richtung = Vector3.back;
anim.SetBool("left",true);;
StartCoroutine (ldelay ());
}
if (drehen == 4) {
drehen = 3;
Richtung = Vector3.back;
anim.SetBool("right",true);
StartCoroutine (delay ());
}
}
if (other.CompareTag ("x+")) {
if (drehen == 1) {
drehen = 2;
Richtung = Vector3.right;
anim.SetBool("right",true);
StartCoroutine (delay ());
Debug.LogWarning("quitted iterator");
}
if (drehen == 3) {
drehen = 2;
Richtung = Vector3.right;
anim.SetBool("left",true);
StartCoroutine (ldelay ());
}
}
if (other.CompareTag ("x-")) {
if (drehen == 1) {
drehen = 4;
Richtung = Vector3.left;
anim.SetBool("left",true);
StartCoroutine (ldelay ());
}
if (drehen == 3) {
drehen = 4;
Richtung = Vector3.left;
anim.SetBool("right",true);
StartCoroutine (delay ());
}
}
}
IEnumerator delay(){
Debug.LogWarning ("in iterator");
yield return new WaitForSeconds (1);
body.velocity = Vector3.zero;
body.angularVelocity=Vector3.zero;
yield return new WaitForSeconds (1);
body.velocity =8*Richtung;
anim.SetBool ("right", false);
rotieren = Quaternion.Euler(Richtung*Time.deltaTime);
}
IEnumerator ldelay(){
Debug.LogWarning ("in iterator");
yield return new WaitForSeconds (1);
body.velocity = Vector3.zero;
body.angularVelocity = Vector3.zero;
yield return new WaitForSeconds (3);
body.velocity = 8 * Richtung;
anim.SetBool ("left", false);
rotieren = Quaternion.Euler(Richtung*Time.deltaTime);
}
}
Answer by KarlKarl2000 · Sep 01, 2016 at 02:45 PM
I had the exact same problem.. I found 5.4 did some weird stuff with my
transform.Translate (Vector3.left * Time.deltaTime)
I found a nifty solution here:
http://answers.unity3d.com/questions/1078784/transformtranslate-not-working-3.html
Then I hacked it to do what I needed. Hope this makes sense and helps others online.
void Update()
{
if(transform.localScale.x > 0) {
_move = new Vector3(4,0,0);
}
if(transform.localScale.x < 0) {
_move = new Vector3(-4,0,0);
}
transform.Translate (_move * Time.deltaTime * 50, Space.World);
}
Sharing is caring
$$anonymous$$y character is a 2D sprite. So when he's
Facing left he's scaled (-1,0,0) Which basically flips him around.
and when
Facing right he's scaled (1,0,0) Which flips him the other direction.
OP was asking why the original
transform.Translate (Vector3.left * Time.deltaTime)
no longer works in 5.4. This was one way I got it working again for my game. But it depends on how you are using transform.Translate.
Hope this answers your question
Sharing is caring
It does answer my question, thank you very much :)
Answer by mj321 · Aug 25, 2016 at 04:38 PM
Did you actually attach that script to an object? I tested it and it worked for me.
Answer by 3dmihai · Aug 25, 2016 at 04:56 PM
The script needs to be attached to your robot as a component. Then try modifying the 0.1f multiplier, probably it moves very slowly.
Answer by Somniothedreamer · Aug 25, 2016 at 05:16 PM
yes id did attached it to two diffrent objects. Maybe i have to change my version of unity. My actual is 5.4.0f3
That's the same version i use. Also, a trivial script like this should work everywhere. Please try again with a new project, just add one cube and attach the script. Even try creating a new script and just paste that one line into the Update() method. $$anonymous$$aybe you broke something trivial by accident.
Follow this Question
Related Questions
Limit movement to just forward/backward and left/right 0 Answers
Moving walls in multiple places 1 Answer
Rigidbody movement instead of transform? 0 Answers
Can someone help me rewriting my script from transform.position to transform.Translate? 1 Answer
How can I move an object between two positions, while it is on a rotating platform? 0 Answers