Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marcell123455 · Jul 18, 2017 at 01:04 PM · c#transformpositionvector

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;
 }
 
 }
 

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marcell123455 · Jul 18, 2017 at 06:34 PM 0
Share

"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..

avatar image Ramlock marcell123455 · Sep 15, 2019 at 12:54 PM 0
Share

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);

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

357 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make cubes between 2 point and stick them together? 0 Answers

Removing Vector3 Decimal for Position 2 Answers

why is transform.position giving me local location?(c#) 0 Answers

Lerp to where the target was x seconds ago? 0 Answers

How do I check if an Object isn't moving? (C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges