- Home /
just update X and Z position of gameobject
Hey Guys, i have a problem, i want to update just the x and z position of a gameobject which has a collider with rigidbody, so that i can move it with the script but that its still on the ground but with transform.position or vector 3 its not working, i know its a easy thing but i dont get it
here is my script (c#)
using UnityEngine; using System.Collections;
public class PlCarStarter : MonoBehaviour { public CanvasGroup ShowCanvas;
public bool Updating = true;
public Transform SpawnPosPlCar;
public GameObject PlayerCar;
public Transform SpawnPosAI1Car;
public GameObject AI1;
public Transform SpawnPosAI2Car;
public GameObject AI2;
public Transform SpawnPosAI3Car;
public GameObject AI3;
public bool CamerasOnOff;
public GameObject Cameras;
void Start () {
ShowCanvas.alpha = 0f;
PlayerCar = GameObject.Find ("Car");
PlayerCar.rigidbody.velocity = Vector3.zero;
PlayerCar.rigidbody.angularVelocity = Vector3.zero;
PlayerCar.transform.position = SpawnPosPlCar.position;
PlayerCar.transform.rotation = SpawnPosPlCar.rotation;
PlayerCar.rigidbody.velocity = Vector3.zero;
PlayerCar.rigidbody.angularVelocity = Vector3.zero;
}
void Update(){
if(Updating == true){
PlayerCar.transform.position = SpawnPosPlCar.position;
PlayerCar.transform.rotation = SpawnPosPlCar.rotation;
PlayerCar.rigidbody.velocity = Vector3.zero;
PlayerCar.rigidbody.angularVelocity = Vector3.zero;
AI1.transform.rotation = SpawnPosAI1Car.rotation;
AI1.transform.position = SpawnPosAI1Car.position;
AI2.transform.rotation = SpawnPosAI2Car.rotation;
AI2.transform.position = SpawnPosAI2Car.position;
AI3.transform.rotation = SpawnPosAI3Car.rotation;
AI3.transform.position = SpawnPosAI3Car.position;
}
if (CamerasOnOff == true) {
Cameras.SetActive(true);
}
}
public void UpdadtOff(){
Updating = false;
}
public void ShowCanvasAlpha(){
ShowCanvas.alpha = 1f;
}
}
Answer by Deathdefy · Jul 18, 2017 at 01:21 PM
If you want to only update specific parts of the position you can do the following.
PlayerCar.transform.position = new Vector3(SpawnPosPlCar.position.x, 0 or whatever your y needs to be set to, SpawnPosPICar.position.z);
Granted you never specified which variable you want to set but you can use the idea above on any Vector3.
"or whatever your y needs to be set to"
my problem is, i dont want to set my y axis to anything
the thing is,
i have a GO with a rigidbody and collider named Car which will be loaded from a other scene, a race. At the start, a animation is running where a transform moves, and changes position. Now i want to make just the x and z axis updated that the cars x and z are = the animated transforms x and z but not the Y because if they are also the same the car is not touching the ground so i want to use the rigidbody of the car to get down. but when i update the y also the car flys over the ground..
For future readers: Just set y to the current position and it will be like it wasn't touched at all.
PlayerCar.transform.position = new Vector3(SpawnPosPlCar.position.x, playerCar.transform.position.y, SpawnPosPICar.position.z);