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 catmandoo · Jan 16, 2015 at 05:57 AM · script.

My player movement rotation script is acting "up"

I have a basic player movement script where the player orients based on his movement, and he totally does face the way he is moving. awesome. however when he isn't moving he returns to face "up" or like north. not awesome.

so how would i fix this? his rotations are frozen on the rigidbody, and i tried kicking up the drag/angular drag super high. i do get a console error when i play saying "Look rotation viewing vector is zero. Unity.engine.Quaternion:LookRotation(vector3,vector3)." which then points to my 4th line of the rotation script.

but I'm not a smart coding man please help D:

also here's the rotation portion of the script.

 void Rotating (float horizontal, float vertical)`enter code here`
     {
         Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
         Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
         Quaternion newRotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
         rigidbody.MoveRotation (newRotation);    
     }
Comment
Add comment · Show 2
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 catmandoo · Jan 16, 2015 at 11:37 PM 0
Share

right, mybad. i guess its also worth noting that i kinda frankenstein'd two movement scripts together to make this lol.

here it is the whole script:

 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     public float speed = 12f;            
     public float turnSmoothing = 15f;
     public float speedDampTime = 0.1f;
 
     Vector3 movement;                   
     Animator anim;                      
     Rigidbody playerRigidbody;          
     //int floor$$anonymous$$ask;                   
     //float camRayLength = 100f;          
     
     void Awake ()
     {
 
         //floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask ("Floor");
         
     
         anim = GetComponent <Animator> ();
         playerRigidbody = GetComponent <Rigidbody> ();
     }
     
     
     void FixedUpdate ()
     {
 
         float h = Input.GetAxisRaw ("Horizontal");
         float v = Input.GetAxisRaw ("Vertical");
         
 
         $$anonymous$$ove (h, v);
         
     
         Rotating (h, v);
         
 
         Animating (h, v);
     }
     
     void $$anonymous$$ove (float h, float v)
     {
 
         movement.Set (h, 0f, v);
         
 
         movement = movement.normalized * speed * Time.deltaTime;
         
 
         playerRigidbody.$$anonymous$$ovePosition (transform.position + movement);
     }
     
     void Rotating (float horizontal, float vertical)
     {
         Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
         Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
         Quaternion newRotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
         rigidbody.$$anonymous$$oveRotation (newRotation);    
     }
     
     void Animating (float h, float v)
     {
 
         bool walking = h != 0f || v != 0f;
         
 
         anim.SetBool ("IsWalking", walking);
     }
 }

avatar image catmandoo · Jan 20, 2015 at 04:36 AM 0
Share

please help im really stuck here ><

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ElDo · Jan 20, 2015 at 05:30 AM

the error you get is absolutely true. You'll get Vector3(0,0,0) which is equal to Vector3.zero if you stop moving in any direction. So have you tried to Change that Piece of code to maybe transform.forward or Vector3(0,0,1) as soon as both horizontal and vertical Input are 0? like that:

 Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
 if(horizontal==0 && vertical==0)targetDirection=new Vector3(0,0,1);
 Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
Comment
Add comment · Show 1 · Share
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 catmandoo · Jan 21, 2015 at 12:25 AM 0
Share

Shoot, i never tried that. it doesn't quite seem to work. but its a step forward and now i know where to look.

Thanks ElDo

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers

Itween 180º curve position and rotation problem 0 Answers

reference variables within my xml file. 0 Answers

Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers

How to Get a Script Component and call a function from another script? 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