- Home /
 
 
               Question by 
               gurbrindersingh · Sep 07, 2020 at 01:10 PM · 
                c#scripting problemcharactercontrollermovement scripttouch controls  
              
 
              How to move and rotate object over slanting surfaces and still keep object's y axis perpendicular to the surface normals?
How to keep gameobject's y axis perpendicular to the surface normals and still keep control of the movement of character on horizonatl and vertical axis? Here is my script: [SerializeField] float raycastLength = 0.25f; //set this to size bool canPlayerMove = true; public float speed = 2f;
     private void Update()
     {
         Ray ray = new Ray(transform.position, -transform.up);
         RaycastHit hitInfo;
 
         if (Physics.Raycast(ray, out hitInfo, 0.5f))
         {
             transform.position = new Vector3(transform.position.x, hitInfo.point.y + 0.25f, transform.position.z);
             Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
             //transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
 
         }
         float moveHorizontal = SimpleInput.GetAxis("Horizontal");
         float moveVertical = SimpleInput.GetAxis("Vertical");
 
         if (canPlayerMove)
         {
             Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
 
             if (movement != Vector3.zero)
             {
                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
             }
 
             transform.Translate(movement * speed * Time.deltaTime, Space.World);
         }
     }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
OnCollisionEnter Sine Wave to Character 0 Answers
question with character controller to move with hitting mousebuttton down 1 Answer
Roll/Dodge/Evade System 1 Answer
Make Character Controler can run with diffrent float 1 Answer
How can make the player move constantly when the key A or D is held down? 1 Answer