• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Rotating my character only while moving in a 2D plane 3 Answers

Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers

How do i rotate my player in moving direction in 2d? 0 Answers

2D Top Down Shooter: AI Ally twitching 2 Answers

How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges