Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigidbody z position will not stay 0

Discussion in 'Scripting' started by caseyboundless, Apr 15, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    When my rigidbody moves it's z position will not stay at 0. It should only move on the x and y positions. I have z rotation and z position constraints ticked on the rigidbody? Any ideas?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveAstroids : MonoBehaviour {
    5.  
    6.     public int astroidSpeed;
    7.     public bool canMoveAst;
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.  
    18.  
    19.         if(transform.position.z >=.1 || transform.position.z <= -.1){
    20.             transform.position = new Vector3(transform.position.x,transform.position.y,0.0f);
    21.         }
    22.         transform.Translate(Vector3.left * Time.deltaTime * astroidSpeed); //move the obj
    23.  
    24.    
    25.    
    26.    
    27.    
    28.    
    29.    
    30.     }
    31.  
    32.  
    33. }
    34.  
     
  2. Gustav_Bok

    Gustav_Bok

    Joined:
    Dec 6, 2012
    Posts:
    35
    Hi,

    Have you tried to put it in "void LateUpdate()" ?
     
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    negative ghost rider! Any other ideas?
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Is its collider hitting something else maybe? I just put your script on object and work just fine, no z movement at all.