Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by TotalityClause · May 27, 2012 at 06:13 AM · rotationobjectspeedreference

Object Rotation/Character Speed

I'm using the Script from the script reference to move the object found here - http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html - And i was wondering how it was that you speed up movement. I changed speed = 6.0F but to no avail, and i changed jumpspeed = 13.0F to change the jump but nothing changed.

How do i change the speed of movement, and how would i change the jump height?

Also,

How would i make an object rotate in the direction of movement? Like, around the axis of before i press the button so it's facing the direction i'm moving.

And, as a bonus, could you explain how each part of that movement script works? I don't understand how it gets what button you're pressing as all it has is is movedirection horizontal... I made my own script, but i was having a few problems with it, so here it is.

using UnityEngine; using System.Collections;

public class WASDMove : MonoBehaviour {

 // Use this for initialization
 
 void Start () {
     
 }

  // Update is called once per frame
 void Update () {
     
     if (Input.GetKey ("w")) 
     {
         rigidbody.AddForce (Vector3.forward * 50);

         }

  if (Input.GetKey ("s"))
     {

         rigidbody.AddForce (Vector3.forward * 45);

         }

 if (Input.GetKey ("a"))

 {

      float translation = Time.deltaTime * 10;
         rigidbody.AddForce (0,0,2);

         }

 if (Input.GetKey ("d"))

  {   

      float translation = Time.deltaTime * 10;
         rigidbody.AddForce (0,0,-2);

     }

 }

} Considering i use if (Input.Getkey ("key")) and that's what tells the program what to do when that is pressed, but the reference one is just horizontal and vertical with no input.

Lots of Love - Totality

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

Answer by aldonaletto · May 27, 2012 at 02:27 PM

1- You're having problems with the Editor's elephant memory: when you attach a script to some object and run, all variables exposed in the Editor (the public variables) have their initial values saved; these values are always loaded in the exposed variables when running, thus modifying their initial values in the script has no effect at all - you must change the values in the Inspector while not running. If you want to register changes made in the script, click the "gear" button at the left of the script component in the Inspector and select option "Reset".
2- The Move example uses keys AD to move the character left/right. These keys are associated to the default "Horizontal" axis in the Input Manager: it returns -1 when A or left arrow is pressed, 1 for D or right arrow, and 0 for none of them. It's a more convenient way to handle inputs: it's simpler, and makes control configuration menus easier to create (the same applies to keys WS and the "Vertical" axis).
3- You can use the mouse to rotate the character: add the script MouseLook.cs to it and select Mouse X in the Axes field. An alternative (Doom style) is to modify the script to use A and D to rotate the character:

C# version:

public float speed = 6.0f; public float jumpSpeed = 8.0f; public float turnSpeed = 90.0ff; // degrees per second public float gravity = 20.0;

// moveDirection is actually the moving velocity: Vector3 moveDirection = Vector3.zero; // cache the CharacterController to improve performance: CharacterController controller;

void Update() { // rotate the character with AD: transform.Rotate(0, Input.GetAxis("Horizontal") turnSpeed Time.deltaTime, 0); // set variable controller only once, instead of each Update: if (!controller) controller = GetComponent< CharacterController>(); if (controller.isGrounded) { // if character is grounded... // use WS to move it in its local forward/backward direction: moveDirection = Input.GetAxis("Vertical") transform.forward speed; // and check Jump (so it can only jump when grounded!):
if (Input.GetButtonDown ("Jump")) { // when Jump pressed... moveDirection.y = jumpSpeed; // apply a positive vertical speed } } // Apply gravity to the vertical speed: moveDirection.y -= gravity Time.deltaTime; // Move the controller with moveDirection speed controller.Move(moveDirection Time.deltaTime); }

JS version:

var speed : float = 6.0; var jumpSpeed : float = 8.0; var turnSpeed : float = 90.0; // degrees per second var gravity : float = 20.0;

// moveDirection is actually the moving velocity: private var moveDirection : Vector3 = Vector3.zero; // cache the CharacterController to improve performance: private var controller : CharacterController;

function Update() { // rotate the character with AD: transform.Rotate(0, Input.GetAxis("Horizontal") turnSpeed Time.deltaTime, 0); // set variable controller only once, instead of each Update: if (!controller) controller = GetComponent(CharacterController); if (controller.isGrounded) { // if character is grounded... // use WS to move it in its local forward/backward direction: moveDirection = Input.GetAxis("Vertical") transform.forward speed; // and check Jump (so it can only jump when grounded!):
if (Input.GetButtonDown ("Jump")) { // when Jump pressed... moveDirection.y = jumpSpeed; // apply a positive vertical speed } } // Apply gravity to the vertical speed: moveDirection.y -= gravity Time.deltaTime; // Move the controller with moveDirection speed controller.Move(moveDirection Time.deltaTime); }

4- I didn't understand your script: are you using a CharacterController or a Rigidbody? Using both in the same object is a recipe for disaster (unless the rigidbody is kinematic, but you couldn't AddForce to it in such case). As a rule of thumb, only use rigidbodies to add a real world physics flavor to objects that need it: cannon balls, grenades, scene objects that may fall, bounce or hit other objects, like crates, barrels etc.

Comment
Add comment · Show 3 · 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 TotalityClause · May 27, 2012 at 04:53 PM 0
Share

I'm using characterController, thanks for all the help! is there an if (Controller.isGrounded) = False; Or something of that nature, to control mid air jumping?

avatar image aldonaletto · May 27, 2012 at 05:38 PM 0
Share

This if:

    ...
    if (controller.isGrounded) { // if character is grounded...
        ...
        if (Input.GetButtonDown ("Jump")) { // when Jump pressed...
            moveDirection.y = jumpSpeed; // apply a positive vertical speed
        }
    }
    ...
only allows jumping when grounded. If you want to jump even while in mid air, move the second if outside the the first one.
avatar image TotalityClause · May 27, 2012 at 05:43 PM 0
Share

No no, i mean when the person has pressed the button to jump, they will be able to move while in midair. As in: While grounded they can move at xSpeed and they can jump, and after jumping they can move at zSpeed (lower than xSpeed) until they hit the ground in which it will return to xSpeed. Would i do the same code without if (controller.isGrounded) so that it only uses the Grounded if it is, infact grounded?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Object Reference Not Set to an Instance of an object 0 Answers

Slowing Down Rotation Speed 1 Answer

Object problem 1 Answer

Object Reference Required to access non-static member 'UnityEngine.RigidBody.AddRelativeTorque' 1 Answer


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