• 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 /
  • Help Room /
avatar image
1
Question by anthony-pinskey · May 11, 2017 at 12:04 AM · raycastingplayer movementrotating

Align Player to Surface Jitters

On my terrain, I set an empty child to to be a collider of it's mesh like below:

alt text

Thanks to some of the community on the Unity forms I was able to get the PlayerController to rotate/hug the slope or terrain smoothly with Racasting as it navigates, but occasionally, certain areas where you rest will jitter like below:

alt text

Here is the code for the PlayerController

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;

     public class PlayerMovement : MonoBehaviour {

     public float speed     = 10f;
     public float runSpeed  = 20f;
     public float jumpForce = 7f;
     public float gravity   = 15f;
     public bool isRunning  = false;
     private Vector3 moveDir = Vector3.zero;
     public bool grounded;
     private Vector3 turnVector;
     private Vector3 posCur = Vector3.zero;
     private Quaternion rotCur;

     // Use this for initialization
     void Start () {

     }

     // Update is called once per frame
     void Update () {
         // CharacterController reference
         CharacterController controller = gameObject.GetComponent<CharacterController> ();
         //Check if character is on the ground
         if( controller.isGrounded ) {
             //Set new Vector3 with X,Y,Z coordinates
             moveDir = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

             moveDir = transform.TransformDirection (moveDir);

             // Check for run input
             if (Input.GetButtonDown ("Run")) {
                 isRunning = true;
             } else if (Input.GetButtonUp ("Run")) {
                 isRunning = false;
             }

             moveDir *= speed;

             // Check for jump input
             if (Input.GetButtonDown ("Jump")) {
                 moveDir.y = jumpForce;
             }
             //Allign player to the terrain
             Ray ray = new Ray(transform.position, -transform.up);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit, 1.5f) == true) {
                Debug.DrawLine(transform.position, hit.point, Color.green);
                rotCur = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
                posCur = new Vector3(transform.position.x, hit.point.y + 1f, transform.position.z);

                grounded = true;
             }
             else {
                grounded = false;
             }
             if(grounded == true) {
                transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime * 5);
             }
             else {
                transform.position = Vector3.Lerp(transform.position, transform.position - Vector3.up * 1f, Time.deltaTime * 5);
                if(transform.eulerAngles.x > 15) {
                    turnVector.x -= Time.deltaTime * 1000;
                }
                else if(transform.eulerAngles.x < 15) {
                    turnVector.x += Time.deltaTime * 1000;
                }
                rotCur.eulerAngles = Vector3.zero;
                transform.rotation = Quaternion.Lerp(transform.rotation, rotCur, Time.deltaTime);
             }
         }
         moveDir.y -= gravity * Time.deltaTime;

         controller.Move (moveDir * Time.deltaTime);

     }

     void FixedUpdate() {
     }

 }

I've tried tweaking speed settings, collider size but I just can't seem to get the jitter to be gone. Any ideas what is going on?

screen-shot-2017-05-10-at-75315-pm.png (360.4 kB)
may-10-2017-20-03-19.gif (201.7 kB)
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

Answer by XenmaHost · Feb 21, 2019 at 07:14 AM

2019... Welp sorry you had nobody to answer you :(

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

105 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

Related Questions

How can we control two player objects at one time with RayCasting? 1 Answer

Quickly rotating game map causes motion sickness 0 Answers

My Ray is going the wrong way 1 Answer

raycast activation by trigger 0 Answers

When Time.timeScale go from 1 to 0 Unity destroy a gameobject 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges