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 uberokeer · Mar 08, 2013 at 08:21 AM · movementcharacteraxisaxes

Character Axes Rotation Problem

Hello, I am kinda new on the Forums and stuff. But I am having a problem I am hoping someone knows the answer to, My problem is: I Need to rotate my Characters Axes to rotate based on the characters Rotation. And i want to do this with a script.

I have My Script Which i used a mouselook script i found on unify : http://wiki.unity3d.com/index.php/Main_Page

My Script is:

 /// This is a modified javascript conversion of the Standard Assets MouseLook script.
 /// Also added is functionallity of using a key to look up, down, left and right in addition to the Mouse.
 /// Everything is on by default. You will want to turn off/on stuff depending on what you're doing.
  
 /// You can also modify the script to use the KeyLook functions to control an object's rotation.
 /// Try using MouseXandY on an object. Actually it works as is but you'll want to clean it up ;)
  
 /// As of this version the key and mouse fight if used at the same time (ie up key and down mouse jitters).
  
 /// Minimum and Maximum values can be used to constrain the possible rotation
  
 /// To make an FPS style character:
 /// - Create a capsule.
 /// - Add a rigid body to the capsule
 /// - Add the MouseLookPlus script to the capsule.
 ///   -> Set the script's Axis to MouseX in the inspector. (You want to only turn character but not tilt it)
 /// - Add FPSWalker script to the capsule
  
 /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
 /// - Add the MouseLookPlus script to the camera.
 ///   -> Set the script's Axis to MouseY in the inspector. (You want the camera to tilt up and down like a head. The character already turns.)
  
 /// - Name an Input element LookUp and assign positive and negative keys to look up and down by key
 /// - Name an Input element LookAround and assign positive and negative keys to look left and right by key
  
     enum Axes {MouseXandY, MouseX, MouseY}
     var Axis : Axes = Axes.MouseXandY;
  
      var movSpeed : float = 45.0f;
      var isAnimation :  boolean;
  
     var sensitivityX = 15.0;
     var sensitivityY = 15.0;
  
     var minimumX = -360.0;
     var maximumX = 360.0;
  
     var minimumY = -60.0;
     var maximumY = 60.0;
  
     var rotationX = 0.0;
     var rotationY = 0.0;
  
     var lookSpeed = 2.0;
  
     function Update ()
     {
     
     if(Input.GetButton("Forward")){
     animation.CrossFade("hum_run");
     transform.position.x += movSpeed * Time.deltaTime;
     isAnimation = true;
     }
     if(Input.GetButton("Backward")){
     animation.CrossFade("hum_back");
     transform.position.x -= movSpeed * Time.deltaTime;
     isAnimation = true;
     }
     if(Input.GetButton("Right")){
     animation.CrossFade("hum_strafe_right");
     transform.position.y += movSpeed * Time.deltaTime;
     isAnimation = true;
     }
     if(Input.GetButton("Left")){
     animation.CrossFade("hum_strafe_left");
     transform.position.y -= movSpeed * Time.deltaTime;
     isAnimation = true;
     }
     if(Input.GetButton("Fire1")){
     animation.CrossFade("hum_attack_1");
     isAnimation = true;
     }
     if(!isAnimation){
     animation.CrossFade("hum_idle");
     }
     if(Input.GetButton("Jump")){
     animation.CrossFade("hum_jump");
     }
     
     
         if (Axis == Axes.MouseXandY)
         {
             // Read the mouse input axis
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  
             // Call our Adjust to 360 degrees and clamp function
             Adjust360andClamp();
  
             // Most likely you wouldn't do this here unless you're controlling an object's rotation.
             // Call our look left and right function.
             KeyLookAround();
  
             // Call our look up and down function.
             KeyLookUp();
  
             // If the user isn't pressing a key to look up, transform our X angle to the mouse.
             if (!Input.GetAxis("LookAround"))
             {
                 // If you don't want to allow a key to affect X, keep this line but take it out of the if
                 transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
             }
  
             // If the user isn't pressing a key to look up, transform our Y angle to the mouse.
             if (!Input.GetAxis("LookUp"))
             {
                 // Multiply the Quaterion so we don't loose our X we just transformed
                 // If you don't want to allow a key to affect Y, keep this line but take it out of the if
                 transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
             }
         }
         else if (Axis == Axes.MouseX)
         {
             // Read the mouse input axis
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
  
             // Call our Adjust to 360 degrees and clamp function
             Adjust360andClamp();
  
             // if you're doing a standard X on object Y on camera control, you'll probably want to 
             // delete the key control in MouseX. Also, take the transform out of the if statement.
             // Call our look left and right function.
             KeyLookAround();
  
             // Call our look up and down function.
             KeyLookUp();
  
             // If the user isn't pressing a key to look up, transform our X angle to the mouse.
             if (!Input.GetAxis("LookAround"))
             {
                 //If you don't want to allow a key to affect X, keep this line but take it out of the if
                 transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
             }
  
         }
         else
         {
             // Read the mouse input axis
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  
             // Call our Adjust to 360 degrees and clamp function
             Adjust360andClamp();
  
             // Call our look left and right function.
             KeyLookAround();
  
             // Call our look up and down function.
             KeyLookUp();
  
             // If the user isn't pressing a key to look up, transform our Y angle to the mouse
             if (!Input.GetAxis("LookUp"))
             {
                 // If you don't want to allow a key to affect Y, keep this line but take it out of the if
                 transform.localRotation = Quaternion.AngleAxis (rotationY, Vector3.left);
             }
  
         }
     }
  
     function KeyLookAround ()
     {
 //      If you're not using it, you can delete this whole function. 
 //      Just be sure to delete where it's called in Update.
  
         // Read the mouse input axis
         rotationX += Input.GetAxis("LookAround") * lookSpeed;
  
         // Call our Adjust to 360 degrees and clamp function
         Adjust360andClamp();
  
         // Transform our X angle
         transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
     }
  
     function KeyLookUp ()
     {
 //      If you're not using it, you can delete this whole function. 
 //      Just be sure to delete where it's called in Update.
  
         // Read the mouse input axis
         rotationY += Input.GetAxis("LookUp") * lookSpeed;
  
         // Adjust for 360 degrees and clamp
         Adjust360andClamp();
  
         // Transform our Y angle, multiply so we don't loose our X transform
         transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
     }
  
     function Adjust360andClamp ()
     {
 //      This prevents your rotation angle from going beyond 360 degrees and also 
 //      clamps the angle to the min and max values set in the Inspector.
  
         // During in-editor play, the Inspector won't show your angle properly due to 
         // dealing with floating points. Uncomment this Debug line to see the angle in the console.
  
         // Debug.Log (rotationX);
  
         // Don't let our X go beyond 360 degrees + or -
         if (rotationX < -360)
         {
             rotationX += 360;
         }
         else if (rotationX > 360)
         {
             rotationX -= 360;
         }   
  
         // Don't let our Y go beyond 360 degrees + or -
         if (rotationY < -360)
         {
             rotationY += 360;
         }
         else if (rotationY > 360)
         {
             rotationY -= 360;
         }
  
         // Clamp our angles to the min and max set in the Inspector
         rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
         rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
     }
  
     function Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
         {
             rigidbody.freezeRotation = true;
         }
     }

It works like a charm, but the problem i am having is: Lets say for example my character is facing forward I can go forward using the key "W" which i went into my input manager and mapped the keys, And basically i have complete w,a,s,d controls but if I am facing forward and then i turn lets just say left, then my character is facing left but my character is still going in the forward direction. So basically I don't know if I am wording this right but, I want to rotate the transform. But keep in mind i want to do it with code. Thanks

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 robertbu · Mar 08, 2013 at 08:56 AM 1
Share

Your problems are in lines 48 - 66. There are a couple of issues here. By convention, forward is almost always positive 'z' but this code move forward on 'x'. You want to make the movement relative. Here is an untested suggestion to get you started.

Change this:

 transform.position.x += movSpeed * Time.deltaTime;

To this:

 transform.position += transform.forward * movSpeed * Time.deltaTime;

And you will need to make similar changes for the other three inputs.

avatar image whydoidoit · Mar 08, 2013 at 09:21 AM 0
Share

@robertbu looks like an answer to me :)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by 3dws · Mar 08, 2013 at 12:11 PM

hi

your problem occur in this part

  if(Input.GetButton("Forward")){
 animation.CrossFade("hum_run");
 transform.position.x += movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Backward")){
 animation.CrossFade("hum_back");
 transform.position.x -= movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Right")){
 animation.CrossFade("hum_strafe_right");
 transform.position.y += movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Left")){
 animation.CrossFade("hum_strafe_left");
 transform.position.y -= movSpeed * Time.deltaTime;
 isAnimation = true;
 }


and i can solve it

when u press W or S must change character position in forward directon not at posiyion.x hen u prees A and D u must change character position in right directin not at position.y for example

           transform.position +=transform.forward* movSpeed * Time.deltaTime;
    

use this if(Input.GetButton("Forward")){ animation.CrossFade("hum_run"); transform.position +=transform.forward* movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Backward")){ animation.CrossFade("hum_back"); transform.position -=transform.forward movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Right")){ animation.CrossFade("hum_strafe_right"); transform.position +=transform.right*movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Left")){ animation.CrossFade("hum_strafe_left"); transform.position -=transform.right*movSpeed * Time.deltaTime; isAnimation = true; }

Comment
Add comment · 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
1

Answer by 3dws · Mar 08, 2013 at 12:11 PM

hi

your problem occur in this part

  if(Input.GetButton("Forward")){
 animation.CrossFade("hum_run");
 transform.position.x += movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Backward")){
 animation.CrossFade("hum_back");
 transform.position.x -= movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Right")){
 animation.CrossFade("hum_strafe_right");
 transform.position.y += movSpeed * Time.deltaTime;
 isAnimation = true;
 }
 if(Input.GetButton("Left")){
 animation.CrossFade("hum_strafe_left");
 transform.position.y -= movSpeed * Time.deltaTime;
 isAnimation = true;
 }


and i can solve it

when u press W or S must change character position in forward directon not at posiyion.x hen u prees A and D u must change character position in right directin not at position.y for example

           transform.position +=transform.forward* movSpeed * Time.deltaTime;
    

use this if(Input.GetButton("Forward")){ animation.CrossFade("hum_run"); transform.position +=transform.forward* movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Backward")){ animation.CrossFade("hum_back"); transform.position -=transform.forward movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Right")){ animation.CrossFade("hum_strafe_right"); transform.position +=transform.right*movSpeed Time.deltaTime; isAnimation = true; } if(Input.GetButton("Left")){ animation.CrossFade("hum_strafe_left"); transform.position -=transform.right*movSpeed * Time.deltaTime; isAnimation = true; }

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 uberokeer · Mar 09, 2013 at 03:05 PM 0
Share

All Right Thanks I will probably play around with this code. Thank You!

avatar image
0

Answer by uberokeer · Mar 08, 2013 at 12:12 PM

Oh Thank you sooooo much this didn't exactly work perfectly but now i know where to tweak it and mess around to get it To work.. Thank You So much! i have been working at this for 2 days xD

Comment
Add comment · 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

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

12 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

Related Questions

character movement follows the direction of camera -1 Answers

pivot of CharacterController`s collider is not the pivot of the charcter???? 0 Answers

Character rotation on foot? 1 Answer

Newbie Question 2 Answers

Z axis change 2 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