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 youngapprentice · Aug 06, 2014 at 09:34 PM · raynormalhill

Get Player to 'Stick' to Surface

Hi, all!

So I have these rolling hills and a player. My goal here is to get the player character to:

  • Align with the surface normal

  • Move smoothly along the surface normal in either direction

That's it for the time being. My current script works, but sometimes the character will miss a face of the hill mesh and walk right through it, and other times the player won't go all the way down to where the surface actually is, giving a floating effect.

Any suggestions? Have any of you tried this before?

Here is my current script:

 public class Locomotion : MonoBehaviour {
     public float moveSpeed;
     public Transform myCharacter;
     private RaycastHit myData;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if( Mathf.Abs ( Input.GetAxis( "Horizontal" ) ) > 0.01){
             //If moving
         float move = Input.GetAxisRaw( "Horizontal" )*moveSpeed*Time.deltaTime;
             //Get a speed and direction
             Vector3 startRay = new Vector3( transform.position.x + 0.1f, transform.position.y + 1.0f, transform.position.z );
             //Get a ray
             Debug.DrawRay( startRay, -transform.up*2.0f );
             //Draw it
             if( Physics.Raycast( startRay, -transform.up*2.0f, out myData )){
                 //If there is a hit
                 myCharacter.transform.rotation = Quaternion.FromToRotation ( Vector3.up, myData.normal );
                 //Align player to surface normal
                 //float newSpeed = 0.01f + Vector3.Angle ( Vector3.up, myData.normal)*move;
                 //Debug.Log ( Vector3.Angle ( Vector3.up, myData.normal));
                 transform.Translate( move, 0, 0, Space.World );
                 //Move player
                 Vector3 newPos = new Vector3( transform.position.x, myData.point.y-0.1f, transform.position.z);
                 //Stick to face on y axis
                 transform.position = newPos;
             }
         }
     
     }
 }
       


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

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Measure 'Steepness' of Normal 1 Answer

how to calculate vector perpendicular to another and apply this to object direction? 2 Answers

Ray-based pathfinding 4 Answers

Select an object with touch (Android) 1 Answer

Casting ray forward from transform position 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