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 ChrisSch · Mar 16, 2013 at 11:27 PM · objectrotatekeysidescrollerpress

Rotate on Key press help?

Hi everyone, thanks for opening this question. xD

So here's the deal. I've been trying to get this to work for weeks with no luck, I went through every related question I found here and googled all over, but couldn't get it to work.

I want to make a sidescroller game, and since I want physics too, I used RigidbodyFPSWalker from the Unify Script Wiki (assuming I need something like that).

Now the first problem arose when I imported the character (tried another too), it was facing a weird downward way, and after rotating it the way I want and applying the script to it the character isn't rotated correctly. And since I don't know how to rotate the object or controller individually from one another I managed to get the controls to work the way I want them by inverting and changing some stuff in the project input settings.

Now then I only mentioned all this cause I think it might interfere with the answer you guys give me. It now moves left (backwards), and right (forward) on key press and jumps. But I want to make it rotate 180 degrees left when pressed left and right when pressed right. Now I spent weeks trying to get this to work but without someone giving me the already done code I can't seem to do anything. I managed to rotate it 180 using this code but the controller rotates too not just the object:

if (Input.GetKey (KeyCode.LeftArrow)) { transform.rotation = Quaternion.AngleAxis(180, Vector3(0,1,0)); } And this is the RigidbodyFPSWalker I used:

 var speed = 10.0;
 var gravity = 10.0;
 var maxVelocityChange = 10.0;
 var canJump = true;
 var jumpHeight = 2.0;
 private var grounded = false;
  
 @script RequireComponent(Rigidbody, CapsuleCollider)
  
 function Awake ()
 {
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
 }
  
 function FixedUpdate ()
 {
     if (grounded)
     {
         // Calculate how fast we should be moving
         var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity);
         targetVelocity *= speed;
  
         // Apply a force that attempts to reach our target velocity
         var velocity = rigidbody.velocity;
         var velocityChange = (targetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
  
         // Jump
         if (canJump && Input.GetButton("Jump"))
         {
             rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
         }
     }
  
     // We apply gravity manually for more tuning control
     rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
  
     grounded = false;
 }
  
 function OnCollisionStay ()
 {
     grounded = true;    
 }
  
 function CalculateJumpVerticalSpeed ()
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2 * jumpHeight * gravity);
 }
 

PS: I apologize if I didn't post the codes the way I should. Its not working the way I wanted to either. xD

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

1 Reply

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

Answer by ChrisSch · Mar 18, 2013 at 01:43 AM

Never mind I solved all of those problems, first one in Blender it self and second with this code found in this question.

 function Update () {
     if (Input.GetButton("Horizontal")||Input.GetButton("Vertical")) {
        var newForward : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
        if (newForward!=Vector3.zero) transform.forward = newForward;
        transform.Translate(Vector3.forward*Time.deltaTime);
     }
 }


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

10 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

Related Questions

How to get raycast and key click info. 1 Answer

Rotating an Object on Key Press! 3 Answers

Key press hides object and spawns another 2 Answers

Rotate Camera Around Player Key PresS 1 Answer

rotating object joystick rotation direction current object independently 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