Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MovementController : MonoBehaviour
- {
- public float
- deadZoneValue = 0.1f,
- angle,
- acceleration = 50.0f;
- public Vector3
- motion ;
- public Vector3
- ground_direction ;
- //--------------------------------------------------------------------------------------------
- void OnGUI()
- {
- GUILayout.Label( "transform.rotation : " + transform.rotation );
- GUILayout.Label( "transform.position : " + transform.position );
- GUILayout.Label( "angle : " + angle );
- GUILayout.Label( "ground_direction : " + ground_direction );
- GUILayout.Label( "motion : " + ground_direction );
- }
- void FixedUpdate ()
- {
- Ray
- ground_check_ray = new Ray( gameObject.transform.position, -gameObject.transform.up );
- RaycastHit
- raycast_result;
- Rigidbody
- rigid_body = gameObject.rigidbody;
- if ( Physics.Raycast( ground_check_ray, out raycast_result ) )
- {
- Vector3
- next_position;
- //UpdateOrientation( gameObject.transform.forward, raycast_result.normal );
- UpdateOrientation( gameObject.transform.forward, raycast_result.normal );
- next_position = GetNextPosition( raycast_result.point );
- rigid_body.MovePosition( next_position );
- ground_direction = raycast_result.normal;
- }
- }
- //--------------------------------------------------------------------------------------------
- private void UpdateOrientation( Vector3 forward_vector, Vector3 ground_normal )
- {
- Vector3
- projected_motion_to_normal_surface = motion - ( Vector3.Dot( motion, ground_normal ) ) * ground_normal;
- Quaternion target;
- if (motion.sqrMagnitude > 0)
- {
- target = Quaternion.LookRotation( projected_motion_to_normal_surface, ground_direction );
- }
- else
- {
- target = Quaternion.LookRotation( transform.forward, ground_direction );
- }
- transform.rotation = Quaternion.Slerp( transform.rotation, target, 0.1f );
- //transform.rotation = Quaternion.LookRotation( projected_forward_to_normal_surface, ground_normal );
- }
- private Vector3 GetNextPosition( Vector3 current_ground_position )
- {
- Vector3
- next_position;
- // //--------------------------------------------------------------------
- // angle = 0;
- // Vector3 dir = this.transform.InverseTransformDirection(motion);
- // angle = Vector3.Angle(Vector3.forward, dir);// * 1f * Time.fixedDeltaTime;
- //
- // if(angle > 0) this.transform.Rotate(0,angle,0);
- // //--------------------------------------------------------------------
- next_position =
- current_ground_position +
- gameObject.transform.up * 0.5f
- + motion
- ;
- return next_position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement