• 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 Benny · Dec 21, 2010 at 09:55 PM · charactercontroller

Why won't Move work for me?

When using this script from the docs for moving character controllers, my character lifts off the ground. Why?

/// This script moves the character controller forward /// and sideways based on the arrow keys. /// It also jumps when pressing space. /// Make sure to attach a character controller to the same game object. /// It is recommended that you make only one call to Move or SimpleMove per frame.

var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) { // We are grounded, so recalculate // move direction directly from axes moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

 }

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

 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);

}

Comment
Add comment · Show 3
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 oliver-jones · Dec 21, 2010 at 10:37 PM 1
Share

Why has this been given a negative feedback? Its a perfectly reasonable question - please remember to leave a comment after leave feedback ...

avatar image Benny · Dec 21, 2010 at 10:46 PM 1
Share

I agree. What's wrong about this question?

avatar image Proclyon · Dec 22, 2010 at 07:49 AM 0
Share

Because I disagree with you, I posted something but not as comment. The reason I think the quality of the question is low I will edit in the post for further explanation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Proclyon · Dec 21, 2010 at 10:32 PM

moveDirection.y = jumpSpeed;

SETS the vertical component of the movement to the jump value.

Now computers don't read minds (yet), so something's gotta set it back to whatever it was before.


moveDirection.y -= gravity * Time.deltaTime;

Decreases it every frame.

What you probably are looking for is

//This is frame 1

if move event occured ==> Increase horizontal movement component of the vector by groundSpeed amount (Increase, not override)

if jump event occured ==> Increase vertical movement component of the vector (Increase, not override)

if gravity event occured ==> Decrease vertical movement component of the vector (Decrease, not override)

//End of the update of this frame

What you are doing now is

if (Input.GetButton ("Jump")) 
{
    moveDirection.y = jumpSpeed;
}

You assigned the value of jumpSpeed. If you are falling at 200 km/h and you would say, well I just jumped, your speed would not be -200 km/h but +jumpSpeed , instead of the result of the sum: "-200km/h + jumpSpeed" , assuming you could jump in the air somehow of course.

Further more as you may have yet noticed, I haven't really answered your question, and actually you may have just wanted something along the lines of "please fix my script", I do not know (No offense meant). I simply felt the need to help you understand the question first. If that makes any sense.

EDIT 12/22/2010 08:49 GMT+1 Clarification on vote down: I think the question was lacking in quality due to primarily the fact that it ends with a "why is this occuring" when a complete copy pasted occured of documented code. It's probably the hello world script of Unity3D. Now that says to me that the user is probably just trying to get one effect no matter how. So it's becoming a "please explain how to program question" OR a "just fix it" question. Both I really dislike since the only person that will learn anything from this Q&A will be the one asking the question. I feel it's a good question if several other users may encounter it and use it, by reference or google search or any other means and not become a dead end.

Also I do not feel it is correct to state:

When using this script from the docs for moving character controllers, my character lifts off the ground. Why?

Without stating what the expected behaviour is

From the comments on my post it looks more like double input registration than anything else. But this question is asking why there is vertical movement.

P.S.

Well the gravity is upside down. It should be "someObject" += Physics.gravity.y or another value. Why? Because gravity is a negative value and double negative is positive. So

( X -= -9.81 ---> X += 9.81 ) Exactly the same.

Here's a test sample included as a Free-B

void Update() { if (GravityOn) { moveDirection = new Vector3(moveDirection.x, moveDirection.y + Physics.gravity.y, moveDirection.z);

  }
  _controller.Move(moveDirection);
  moveDirection = Vector3.zero;

}

Mind you that this is NOT affected by scaling. Huge objects will SEEM to fall slower, but that happens in the real world aswell. Similarly you can say that you may not want to use gravity with -9.81 all the time it can seem pretty fast at times. Also be carefull when jumping, you need to ADD gravity + jumpheight, otherwise you need to calculate how much effort you need to overcome gravity all the time, when in fact when you change global gravity you probably want to maintain the actual jumpheight (Most of the time, not like the UT low grav. mode)

Comment
Add comment · Show 4 · 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 Benny · Dec 21, 2010 at 10:39 PM 0
Share

No, no, it's just that the script from the docs isn't working and I don't know why. I actually cut out the jumping part for my own reasons. I guess I left it in there. My bad, I'll fix that. What the problem really is, is that when I activate horizontal and vertical axises, my character goes up as well as forwards, backwards, left or right. :/

avatar image Proclyon · Dec 21, 2010 at 11:16 PM 0
Share

Yes but that's what it is supposed to do right? The default settings in the input manager have the axis defined for positive button (increase along axis) such as "right" increase the value of the moveDirection Vector whilst on the same virtual button called "horizontal" the left button would call this button but give the same negative value instead.

avatar image Proclyon · Dec 21, 2010 at 11:18 PM 0
Share

Idea: If you want to change the input , use the -->Preferences-->Input in the Unity3D editor and make a button with just a positive value and call it "right" or "left" rather then doing both at the same time. Or simply remove the negative value from the existing "horizontal" and "vertical" button in the input manager

avatar image Benny · Dec 21, 2010 at 11:19 PM 0
Share

No, like up along the y axis.

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

How to detect if characterController is facing a wall? 1 Answer

First person dash advice 0 Answers

Locomotion System Basics 1 Answer

Double collision with Character Controller? 3 Answers

Character Controller by text sentences 1 Answer

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