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 OmarKronos · Sep 04, 2012 at 03:09 AM · movementcharactercontrollerlerpslerprotatetowards

How to use Slerp / Lerp / RotateTowards to rotate an object (from-to) two rotations but around their Y axis only?

I've created a script for character movement from scratch .. and it has just worked as perfect as i wanted, but I had a real issue..

in my script I've relied for movement on Slerp , Lerp and RotateTowards functions for Quaternions .. I mean I've tried the three of them and i got the same result of rotating and Looking towards camera just as I wanted .. The Problem is : my character is rotating around all the axis of my Orbit Camera [the target rotation] .. I just need it to be rotated only around a Y Axis of Orbit Camera so that when I rotate my camera up and down it doesn't affect my character to rotate down with it as well .. Take a look at my script :

 var Speed         : float = 6;
 var Gravity           : float = 20;
 var rotationSpeed     : float = 5;
 var Angle           : float;
 var MainCamera         : Transform;
 
 function Update() {
 
 controller = GetComponent(CharacterController);
 
 if (controller.isGrounded)
 {    
            moveDirection = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
         moveDirection = MainCamera.TransformDirection(moveDirection);
         moveDirection *= Speed;
                  
         moveDirection.y = -10;
         
         if (controller.velocity.sqrMagnitude > 0.1)
            transform.rotation = Quaternion.Slerp(transform.rotation,MainCamera.rotation*Quaternion.Euler(0,Angle,0),Time.deltaTime*rotationSpeed);
         //
         if (Input.GetAxis("Vertical") >= 0.01)
         {
         Angle = 0;
         
         if (Input.GetAxis("Horizontal") >= 0.01)
         Angle = 45;
         
         if (-Input.GetAxis("Horizontal") >= 0.01)
         Angle = -45;
         }
            //    
         if (Input.GetAxis("Horizontal") >= 0.01)
         {
         Angle = 90;
         
         if (Input.GetAxis("Vertical") >= 0.01)
         Angle = 45;
         
         if (-Input.GetAxis("Vertical") >= 0.01)
         Angle = 135;
         }
         //
         if (-Input.GetAxis("Vertical") >= 0.01)
         {
         Angle = 180;
         
         if (Input.GetAxis("Horizontal") >= 0.01)
         Angle = 135;
         
         if (-Input.GetAxis("Horizontal") >= 0.01)
         Angle = -135;
         }
         //                                                                                                 
         if (-Input.GetAxis("Horizontal") >= 0.01)    
         {        
         Angle = -90;        
         
         if (Input.GetAxis("Vertical") >= 0.01)
         Angle = -45;
         
         if (-Input.GetAxis("Vertical") >= 0.01)
         Angle = -135;        
         }            
 }      
 
 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);
 }
 
 function ApplyGravity () {
     moveDirection.y -= Gravity * Time.deltaTime;
 }

Please , help me in my problem within these three functions Slerp or Lerp or RotateTowards, Thank you.

here's a sample of the game on the web so that you could see the problem by your selves : http://www.4shared.com/rar/jdycCqeS/The_Sample_Game.html?

Comment
Add comment · Show 1
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
avatar image asafsitner · Sep 04, 2012 at 06:49 AM 0
Share

How about you lerp only the Y value?

Use `Quaternion.LookRotation` to calculate the necessary rotation that looks at the camera, convert it to euler angles with `Quaternion.eulerAngles`, then lerp the character's `transform.eulerAngles.y` to the rotation's y value.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use Lerp? 3 Answers

Movement Smoothing Script not Working 2 Answers

Character too floaty after jumping and grappling.,Grapple too floaty 0 Answers

slerp vs lerp for rotation 2 Answers

Character Controller Movement 0 Answers


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