Search Unity

Why the object is not moving only on the x ?

Discussion in 'Scripting' started by DubiDuboni, Sep 5, 2019.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    In the editor I have a parent object and it's Y rotation set to 15
    Under this I have some children.

    Then I have another object that is not child of the parent also rotation set to 15 and position on the parent position in the center. When I drag the object with the red arrow in the editor in the scene view it's moving only on the x fine.

    But in the game when I click the mouse button I see for some reason that the object is moving on x but also on the z.


    But now I'm clicking once on the Video cube(button) in the game window and the object VideoAudioCube is moving again only on the X but for some reason even if the Z not changing I see that the object is also moving on the Z .


    But when the VideoAudioCube is moving when I click the Video it's moving only on the X at least I see in the editor only the X changing but it's moving also on the Z:

    This is the code the code is attached to the New Game cube gameobject a child under Buttons :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class VideoSettings : MonoBehaviour
    7. {
    8.     public MeshRenderer VideoAudioCube;
    9.     public float speed = 5f;
    10.     public float distanceToMove;
    11.  
    12.     private bool keepMoving = true;
    13.     private Vector3 startPos;
    14.     private Vector3 endPos;
    15.     private bool moveBack = false;
    16.  
    17.     private void Start()
    18.     {
    19.         startPos = VideoAudioCube.transform.position;
    20.     }
    21.  
    22.     private void OnMouseDown()
    23.     {
    24.         if (keepMoving == true)
    25.         {
    26.             if(VideoAudioCube.transform.position == startPos)
    27.             {
    28.                 moveBack = false;
    29.             }
    30.  
    31.             if (transform.GetComponentInChildren<TextMesh>().text == "Video")
    32.             {
    33.                 StartCoroutine(MoveVideo());
    34.             }
    35.  
    36.             keepMoving = false;
    37.         }
    38.     }
    39.  
    40.     private IEnumerator MoveVideo()
    41.     {
    42.         endPos = VideoAudioCube.transform.position + Vector3.right * distanceToMove;
    43.  
    44.         VideoAudioCube.enabled = true;
    45.  
    46.         if (moveBack == false)
    47.         {
    48.             while (VideoAudioCube.transform.position != endPos)
    49.             {
    50.                 VideoAudioCube.transform.position =
    51.                   Vector3.MoveTowards(VideoAudioCube.transform.position, endPos, Time.deltaTime * speed);
    52.                 yield return null;
    53.             }
    54.         }
    55.         else
    56.         {
    57.             while (VideoAudioCube.transform.position != startPos)
    58.             {
    59.                 VideoAudioCube.transform.position =
    60.                     Vector3.MoveTowards(VideoAudioCube.transform.position, startPos, Time.deltaTime * speed);
    61.                 yield return null;
    62.             }
    63.         }
    64.  
    65.         keepMoving = true;
    66.         moveBack = true;
    67.     }
    68. }
    69.  
    The Main Camera rotation is set to 0,0,0 and positioned in front of Buttons.
     
    Last edited: Sep 6, 2019
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Found the problem : The endPos should be transform.tight instead Vector3.right :

    Instead this line :

    Code (csharp):
    1.  
    2. endPos = VideoAudioCube.transform.position + Vector3.right * distanceToMove;
    3.  
    It should be like this :

    Code (csharp):
    1.  
    2. endPos = VideoAudioCube.transform.position + VideoAudioCube.transform.right * distanceToMove;
    3.