• 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
2
Question by Sigurd M · May 26, 2010 at 05:23 PM · 2drotationmovement

Character rotation problem!

Hi! I'm new to scripting and working hard to learn Javacript. I'm writing a movement script for a 2D-sidescroller. I made this with scripts from tutorials and what I've found in forums. Movement works exactly how i want it to, but i cant get the controller to rotate along its y-axis when turning to walk the other way (You will find my attempt at the end of the script).

I would really appreciate it if you could help me out here!:D

Sigurd

Movement.js

var gravity = 20.0;

private var moveDirection = Vector3.zero; private var grounded = false;

class ControllerMovement {

var speed = 6.0; var jumpSpeed = 8.0; var rotationSmoothing = 10.0;

@System.NonSerialized var direction = Vector3.zero;

// Are we jumping? (Initiated with jump button and not grounded yet) @System.NonSerialized var jumping = false;

// This keeps track of our current velocity while we're not grounded? @System.NonSerialized var inAirVelocity = Vector3.zero;

}

var movement : ControllerMovement;

private var controller : CharacterController;

function FixedUpdate () {

transform.position.z = 0;

}

function Update() { if (!grounded) { moveDirection.x = Input.GetAxis("Horizontal") movement.speed;
} // If grounded, zero out y component and allow jumping else { moveDirection = Vector3(Input.GetAxis("Horizontal")
movement.speed, 0.0, 0.0);

if (Input.GetButton ("Vertical")) { moveDirection.y = movement.jumpSpeed; } } moveDirection = transform.TransformDirection(moveDirection);

// Apply gravity moveDirection.y -= gravity * Time.deltaTime;

// Move the controller grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) & CollisionFlags.CollidedBelow) != 0;

// Set rotation to the move direction
if (movement.direction.sqrMagnitude > 0.01) transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);

// We are in jump mode but just became grounded if (controller.isGrounded) { movement.inAirVelocity = Vector3.zero; if (movement.jumping) { movement.jumping = false; SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver);

     var jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity;
     if (jumpMoveDirection.sqrMagnitude > 0.01)
         movement.direction = jumpMoveDirection.normalized;
 }



}

}

@script RequireComponent(CharacterController)

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
1
Best Answer

Answer by Sigurd M · May 30, 2010 at 05:28 AM

Figured it out myself:D

  1. Take the PlatformerController.js from the 2D Sidescroller-tutorial

  2. Add: private var grounded = false;

  3. Right under "// Grounded controls", replace; "if (controller.isGrounded)" with "if (!grounded)".

Play with the settings to get the desired movement. This gives sort of ( not exactly the same because of the extra hight-jump) movement as in the script above, but with rotation.

Sigurd

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer

Unity 2D: How can i get my sprite to rotate to the direction I am going? 1 Answer

2D Simulated Sword Swing - Sword Floating Away 0 Answers

2D Zig-zag movement and rotation 1 Answer

2D rotation around a object 4 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