Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Slence · May 30, 2014 at 01:32 PM · movementposition

Ignoring the transform.y position on movement

For example, I have 2 gameobjects. I have a script where gameobject1 follows gameobject 2 using this function:

 transform.LookAt(player.position);
 controller.SimpleMove(transform.forward * speed);

This works fine, but when I add more gameobjects, that also moves towards gameobject 1, it starts bugging out on the Y position, resulting the gameobject starts to fly above gameobject 1.

Is there some way to make sure every gameobject always stays on the ground (transform.position.y = 0)?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by PouletFrit · May 30, 2014 at 03:04 PM

Since you are using SimpleMove, gravity is applied. So my guess is that your problem come from the lookAt which apply a rotation on the x and/or z axis. But you probably only want to look forward the direction in which your moving (you only want rotation on the y axis).

So you have two possibilities:

1) You only keep the rotation on y axis:

 transform.LookAt(player.position);
 Vector3 rot = player.eulerAngles;
 rot.x = 0;
 rot.z = 0;
 player.eulerAngles = rot;

2) Or you can use Vector3.RotateTowards, in which you also can "gradually" rotate towards ur target:

 float step = speed * Time.deltaTime;
 
 Vector3 from = new Vector3(transform.position.x,0,transform.position.z);
 Vector3 to = new Vector3(player.position.x,0,player.position.z);
 transform.eulerAngles = Vector3.RotateTowards(from,to,step,0.0f);
 
Comment
Add comment · Show 4 · 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 Slence · May 30, 2014 at 03:47 PM 0
Share

That works well for the rotation, but my problem also was the Y position coordinate. The gameobject still floats into the air. This is what I'm using right now:

 transform.LookAt(player.position);
     Vector3 rot = player.eulerAngles;

     rot.x = 0;
     rot.z = 0;

     player.eulerAngles = rot;
     controller.Simple$$anonymous$$ove(transform.forward * speed);
avatar image PouletFrit · May 30, 2014 at 08:07 PM 0
Share

Is it feeling like a character is able to just gradualy walk on top of another one? If yes, it might be that your collider aren't set properly.

Try adjusting their height and you can always set the slope Limit of the character controller to a higher number.

And of course, you can always use what Landern proposed, it is pretty straighforward, but it should definitly work: just set position.y to 0 (or whatever height is ur floor) after each movement, that way your are sure that your character will always have it's y to 0.

avatar image Slence · May 30, 2014 at 08:36 PM 0
Share

Ugh, I appreciate your help a lot but I can't get it to work properly. I tried (just in case) increasing the slope limit of the game objects. I tried setting the height always to 0 with: alwaysGround = gameObject.transform.position.y; Collider height is a bit strange to me though, it means that if I set it higher then my character will always fly.

And yes, the gameobjects are gradualy walking on top of others

avatar image PouletFrit · May 31, 2014 at 12:52 AM 0
Share

have you tryed to simply add

 transform.position = new Vector3(transform.position.x, 0, transform.position.z);

right after controller.Simple$$anonymous$$ove?

avatar image
0

Answer by Landern · May 30, 2014 at 01:46 PM

If there is a rigidbody attached, either through code or the inspector you can freeze rotation and position.

RigidBody Constraints

Comment
Add comment · Show 5 · 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 Slence · May 30, 2014 at 02:07 PM 0
Share

I'm not sure but when I added the RigidBody and Constrained the X position, the gameobject moves really laggy because it is still calling the Simple$$anonymous$$ove function. Sorry for being new, but how do I fix it?

avatar image PouletFrit · May 30, 2014 at 02:44 PM 0
Share

you shouldn't use a rigidbody with a controller, normally it produce weird result

avatar image Landern · May 30, 2014 at 02:46 PM 0
Share

PouleFrit is correct.

avatar image Slence · May 30, 2014 at 02:53 PM 0
Share

Okay but how do I fix this problem then without using a rigidbody?:/

avatar image Landern · May 30, 2014 at 02:54 PM 0
Share

$$anonymous$$odify the Simple$$anonymous$$ove method and ensure that the position.y is always 0 when updating the position.

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

22 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

Related Questions

Moving an Object to the vector of other objects on button press using a vector3 array? 0 Answers

How to Have Projectile Reset to Initial Position Relative to Parent 1 Answer

Movement with iTween and parameter 0 Answers

How to control an object with a mouse? 1 Answer

how can do click then move on a gameobject? 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