Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 MC HALO · Jul 03, 2011 at 02:29 AM · fps

What is wrong with this movement script?

Hi guys i created a script for my FPS player which allowed him to move left and right and up and down and etc. No the problem was my old script avoided objects and did not have any gravity added to it. So i posted a question and i was told if i use this it would work as well and give my player gravity and etc.

Now if i press the up arrows or the wsda keys the player will not move he will fly up in the air why is that ?

here is the new script :

/// 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.zero;
 function Start(){
 
 
   gravity = 20;
 
 
 }
 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;
         
         if (Input.GetKey(KeyCode.Space)) {
             moveDirection.y = jumpSpeed;
         }
     }
 
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
     
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 } 

i would be thank full if someone could help me :)

ps this was my old script which was working find but no gravity and collision. i am using a character controller by the way. Old Script:

 public var MoveSpeed: float = 15;
 
 public var MoveLSpeed : float = 5;
 
 function Update () {
 if (Mathf.Abs(Input.GetAxis("Vertical")) > 0){
      var MoveFWD : float =  Input.GetAxis("Vertical")* Time.deltaTime * MoveSpeed ;
 
      transform.Translate(Vector3.forward * MoveFWD);
 }
 
 if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0){
 
      var MoveLeft : float = Input.GetAxis("Horizontal")* Time.deltaTime * MoveLSpeed;
      transform.Translate(Vector3.left * MoveLSpeed);
 
 }
 }
 
     @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
2
Best Answer

Answer by aldonaletto · Jul 03, 2011 at 02:57 AM

Well, there's nothing wrong with the script, but due to some reason it's not working for you. I merged your older script and added a small velocity down the Y axis, then used Move instead of translate. It worked with a simple cylinder which had a Character Controller instead of a collider.

 public var MoveSpeed: float = 15;
 public var MoveLSpeed : float = 5;
 
 function Update () {
     var MoveFWD : float = Input.GetAxis("Vertical")* Time.deltaTime * MoveSpeed ;
     var MoveLeft : float = Input.GetAxis("Horizontal")* Time.deltaTime * MoveLSpeed;
     var MoveDown : float = Time.deltaTime * -10; // applys a small vert velocity
     var controller = GetComponent(CharacterController);
     controller.Move(Vector3(MoveLeft, MoveDown, MoveFWD));
 }
 
 @script RequireComponent (CharacterController)
Comment
Add comment · Show 1 · 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 MC HALO · Jul 03, 2011 at 03:07 AM 0
Share

Yep fantastic that solved it :0 cheers :) thank you so much

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why does my code works? 0 Answers

How to get bullets to hit crosshair 2 Answers

Is Unity Tutorials messing up my project? 1 Answer

How can I stop the fps controller jumping up slopes? 1 Answer

unlocking weapon (fps) 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