Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Making a character look in the direction but also align with ground normal.

Discussion in 'Scripting' started by GuyStreamsStuff, Nov 26, 2018.

  1. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    So I got this script that's working almost as intended. Only thing that's going wrong is that the character's final rotation seems ingame as the average of the rotations between the controlstick-to-rotation part and the ground-normal-to-rotation part. I figured that the stick to rotation part should only affect the local y axis of rotation but I have no idea on how to do that. Any suggestions?

    Thanks for the help guy! This forum and its members are always giving me great idea and solutions. Hopefully I'll be able to return the favor!
    Code (CSharp):
    1.         if (Physics.Raycast(frontRay, out hitFront, 1.3f) && Physics.Raycast(backRay, out hitBack, 1.3f))
    2.         {
    3.             newPosition = new Vector3(transform.position.x, (hitFront.point.y + hitBack.point.y) / 2 + transform.up.y, transform.position.z);
    4.             newRotation = Quaternion.FromToRotation(transform.up, ((hitFront.normal + hitBack.normal).normalized)) * transform.rotation;
    5.         }
    6.  
    7.         combinedAxises = Mathf.Clamp(Mathf.Abs(Input.GetAxis("Horizontal")) + Mathf.Abs(Input.GetAxis("Vertical")), 0f, 1f);
    8.  
    9.         if (Input.GetAxis("Horizontal") != 0f || Input.GetAxis("Vertical") != 0f)
    10.         {
    11.             var moveDirection = Vector3.zero;
    12.  
    13.             moveDirection += cam.transform.forward * Input.GetAxis("Vertical");
    14.             moveDirection += cam.transform.right * Input.GetAxis("Horizontal");
    15.             moveDirection.y = 0f;
    16.  
    17.             var newRotation = Quaternion.LookRotation(moveDirection);
    18.  
    19.             transform.localRotation = Quaternion.Slerp(transform.rotation, newRotation, rotationSpeed * rotationSpeedModifier * Time.deltaTime);
    20.  
    21.             sonicVelocity = transform.InverseTransformDirection(GetComponent<Rigidbody>().velocity);
    22.             sonicVelocity.x *= 0.1f;
    23.             GetComponent<Rigidbody>().velocity = transform.TransformDirection(sonicVelocity);
    24.  
    25.         }
    26.  
    27.         if (Physics.Raycast(frontRay, out hitFront, 1.3f) && Physics.Raycast(backRay, out hitBack, 1.3f))
    28.         {
    29.             transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * 250);
    30.             transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 20f * Time.deltaTime);
    31.         }
    32.  
     
  2. GuyStreamsStuff

    GuyStreamsStuff

    Joined:
    Dec 22, 2017
    Posts:
    27
    Solved it by taking
    newRotation
    , transforming it to euler angles, modifying the x and z axis to the
    transform.rotation.eulerangles
    corresponding to those axis.