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 rickburgen · Feb 16, 2013 at 08:38 PM · movementtransformterrainmove

Moving on uneven terrain issue

Hi All:

I am starting my first Unity first person game. I have an fbx terrain that has a "ramp" to another part of the level. And currently moving the character around using the Translate function. The terrain and player both have mesh colliders attached. Now when the character moves, it is stuck in the same y position. Which I understand why that's happening, so my question is how can I change the y position based on the terrain. So far I have tried the below code which I intend for it to place the character above the ground; with out luck. Can someone point me the right direction?

                 transform.Translate(characterLoc * (input.Running() ? walkSpeed * runMultiplier : walkSpeed));
         
         transform.Rotate(characterRot * rotationSpeed);
         
         RaycastHit hit;
         if (Physics.Raycast(transform.position, -Vector3.up, out hit)) {
             if (hit.collider.gameObject.name == "ground") {
                 float distanceToGround = hit.distance;
                 transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));
             }
         }
         else if (Physics.Raycast(transform.position, Vector3.up, out hit)) {
             if (hit.collider.gameObject.name == "ground") {
                 float distanceToGround = hit.distance;
                 transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));
             }
         }
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

3 Replies

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

Answer by rickburgen · Feb 25, 2013 at 09:32 PM

The issue was that I was using two mesh colliders one for the terrain and one for the player. Oppps. Noob mistake I guess.

Comment
Add comment · 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
0

Answer by MountDoomTeam · Feb 16, 2013 at 10:13 PM

is your character just staying on the same Y with that script?

instead of the line

 transform.Translate(new Vector3(transform.position.x, transform.position.y + distanceToGround, transform.position.z));

you can write

 transform.position.y = hit.point.y + (height of legs);

I think that would make an object always stuck to the floor under it as if it had 1000000000 gravity:))

Comment
Add comment · Show 1 · 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 rickburgen · Feb 16, 2013 at 10:34 PM 0
Share

Ok, I tried that out but didn't work. The raycast is co$$anonymous$$g back false for both checks, so none of that is even being executed. Guess I should have checked that first, but I'm still unsure how to proceed.

Here is a screen shot of my terrain with the player stuck in the Y axis (the green is the top of the terrain) alt text

screen shot 2013-02-16 at 5.24.17 pm.png (43.8 kB)
avatar image
0

Answer by RyanZimmerman87 · Feb 16, 2013 at 11:47 PM

You should be able to get pretty accurate movement just with a simple capsule collider on the character. As long as you are using a Rigidbody with gravity enabled.

This will solve your y-axis floating problem, but you may begin to experience slight sliding effects on the x and y axis if you are on a hill. So you might need to cast a ray downwards and then manually keep the character in that spot unless they use movement keys.

I never used their built in character controller features, because when I first started I was so new at coding that I wanted to learn it from scratch.

But as far as I can tell it takes a lot of extra work to set up realistic movements from scratch. Kind of disappointing considering 99% of games probably require this. Perhaps their character controller features would solve this but I never tried it.

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 rickburgen · Feb 17, 2013 at 12:36 AM 0
Share

So here is what I noticed as I did some more debugging. The second raycast was being called, but the collider was that character itself. Weird!! Is this because the character has vertical height?

There is no rigidbody or character controller attached, as I am trying to make my own basic controller and there is no need for the physics engine.

avatar image rickburgen · Mar 08, 2013 at 09:51 PM 0
Share

Can there be an issue with the way the models were imported?

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

11 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

Related Questions

Problems with the movement of the player 1 Answer

How can i set a radius for rotatearound ? 0 Answers

How do I check where my character is moving before he moves there? 1 Answer

Stop Object From Moving After Key Release 2 Answers

How I start to create a person with movement ? 2 Answers


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