Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sapien_ · Apr 26, 2018 at 09:58 PM · rotationscript.charactercontrollerdirectionwalljump

Controller Rotation returns to Zero when there is no movement.

I have managed to get my character to face the direction he is moving in, however when the character is not moving, He faces his start position. All of the rotation values return to 0. Why does this happen?

Also how do I get my character to face the direction of my inputs in the air?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Playables;
 
 public class Aerial_Controler : MonoBehaviour {
 
     public CharacterController MyController;
     // Update is called once per frame
     public float GroundSpeed = 7f;
     public float CurrentSpeed;
     public float SprintSpeed = 10f;
     public float TimeZeroToMax = 2.5f;
     float accelRatePerSec;
     public float AerialSpeed = 2f;
     public float GravityStrength = 5f;
     public float JumpSpeed = 10f;
     public Transform CameraTransform; 
     bool canJump = false;
     float verticalVelocity;
     Vector3 velocity;
     Vector3 groundedVelocity;
     Vector3 normal;
     bool onWall = false;
 
     private void Start()
     {
         accelRatePerSec = SprintSpeed / TimeZeroToMax;
         CurrentSpeed = 0f;
         MyController = GetComponent<CharacterController>();
 
     }
 
 
 
     void Update()
     {
         bool run = Input.GetKey(KeyCode.M);
 
 
         if (run)
         {
             CurrentSpeed += accelRatePerSec * Time.deltaTime;
             CurrentSpeed = Mathf.Min(CurrentSpeed, SprintSpeed);
         }
 
         else
         {
             CurrentSpeed = GroundSpeed;
         }
 
         if (Input.GetKeyUp(KeyCode.M))
         {
             CurrentSpeed += accelRatePerSec * Time.deltaTime;
             CurrentSpeed = Mathf.Max(GroundSpeed, CurrentSpeed);
         }
 
         if (run && !MyController.isGrounded)
         {
             CurrentSpeed = GroundSpeed + AerialSpeed;
         }
 
         Physics.IgnoreLayerCollision(9, 10);
 
         Vector3 myVector = Vector3.zero;
 
         //get input from the player
         Vector3 input = Vector3.zero;
         input.x = Input.GetAxis("Horizontal");
         input.z = Input.GetAxis("Vertical");
         input = Vector3.ClampMagnitude(input, 1f);
         transform.rotation = Quaternion.LookRotation(groundedVelocity);
 
 
         if (MyController.isGrounded)
         {
             myVector = input;
             Quaternion inputRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(CameraTransform.forward, Vector3.up), Vector3.up);
             myVector = inputRotation * myVector; //rotate input by direction of the camera
             myVector *= CurrentSpeed;
         }
         else
         {
             Quaternion inputRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(CameraTransform.forward, Vector3.up), Vector3.up);
             input = inputRotation * input;
             myVector = groundedVelocity;
             myVector += input * AerialSpeed;
         }
 
         myVector = Vector3.ClampMagnitude(myVector, CurrentSpeed);
         myVector = myVector * Time.deltaTime;
 
 
         verticalVelocity = verticalVelocity - GravityStrength * Time.deltaTime;
         if (Input.GetButtonDown("Jump"))
         {
             if (onWall)
             {
                 Vector3 reflection = Vector3.Reflect(velocity, normal);
 
                 Vector3 projected = Vector3.ProjectOnPlane(reflection, Vector3.up);
                 groundedVelocity = projected.normalized * CurrentSpeed + normal * AerialSpeed;
             }
             //add Jumped to vertical velocity
             if (canJump)
                 verticalVelocity += JumpSpeed;
         }
         myVector.y = verticalVelocity * Time.deltaTime; //add new speed, to old speed, for acceleration
 
         //use input to move the character
         CollisionFlags flags = MyController.Move(myVector);
         velocity = myVector / Time.deltaTime;
         print(velocity);
         //use flags to determine if character can jump or not
         //if on ground
         //set canJump to true
         if ((flags & CollisionFlags.Below) != 0)
         {
             groundedVelocity = Vector3.ProjectOnPlane(velocity, Vector3.up);
             canJump = true;
             verticalVelocity = -3f;
             onWall = false;
         }
         else if ((flags & CollisionFlags.Sides) != 0)
         {
             canJump = true;
             onWall = true;
         }
         else
         {
             canJump = false;
             onWall = false;
         }
     }
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         normal = hit.normal;
     }
  
 }
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 Sapien_ · Jun 09, 2018 at 10:42 PM 0
Share

Wow i forgot about this. I still haven't figured it out. All I know is that it has something to do with the Quaternion.LookRotation.

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

184 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 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

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

Sliding in only one direction 1 Answer

Rotation problems 0 Answers

Help with player rotation on rotating sphere 1 Answer

How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 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