Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 arianloco · Jul 18, 2014 at 09:36 AM · bugnoobx axisnothing

My Player flys off away through the X axis when I press play.

What's going on? I want the player to be able to move the x and Z and jump, but I've had some trouble getting the Z to work.

 @script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
  
 //public variables
  
 var maxAirJumpCount : int = 1; // 1 for a double jump
  
 var normalJumpForce : float = 8; //strength of the normal jump's impulse
 var airJumpForce : float = 8; //strength of the double jump's impulse
  
 var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
  
 var xSpeed : float = 5; // horizontal speed of the character\
 
 var zSpeed : float = 5; //zspeed
  
 /* These values are all ideal settings that worked for me */
  
 //private variables
  
 private var airJumpCount : int = 0; //how many more times can the player jump?
  
 private var xMove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
                             // horizontal speed modifier (xSpeed)
  
 private var isGrounded : boolean = false; //is the player on the ground?
  
 function Update() {
  
     if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
  
         isGrounded = true;
         airJumpCount = maxAirJumpCount;
     }
     else {
  
         isGrounded = false;
     }
  
     if(Input.GetKeyDown(KeyCode.Space)) {
  
         if(isGrounded) {
             JumpNormal();
         }
         else if(airJumpCount > 0) {
  
             airJumpCount -= 1;
             JumpInAir();
         }
     }
  
     xMove = 0;
     xMove = Input.GetAxisRaw("Vertical") * xSpeed;
         
         z = Input.GetAxisRaw("Horizontal") * zSpeed; 
         transform.Translate(0,0,0); 
  
             }
  
 function FixedUpdate () {
     rigidbody.AddForce(Vector3.right*xMove);
 }
  
  
 function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, ForceMode.Impulse); }
  
 function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, ForceMode.Impulse); }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by codestage · Jul 18, 2014 at 09:44 AM

I can't see how you use z variable in your code. Looks like it should be used in Translate(). Are you sure you really wish to make such translate:

 transform.Translate(0,0,0);

? It does nothing.

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 arianloco · Jul 18, 2014 at 01:28 PM 0
Share

I just need to edit the code to make Z move like X, by pressing the horizontal buttons. but I thought what I had was correct.


avatar image
0

Answer by Fearthefrozt · Jul 18, 2014 at 04:35 PM

First you don't need to do this:

xMove = 0;

The function for getting the axis will return 0 if nothing is being pressed so you don't really need it.

Next you're using transform.Translate which you don't want to do if you're moving with physics so you can take out the line:

transform.Translate(0,0,0);

What you're really interested in is this:

function FixedUpdate () { rigidbody.AddForce(Vector3.right*xMove); }

this line is what is actually moving your player, so you want to do a similar thing with the z, so since you're using the vertical axis I'm assuming you want to move forward and backward with the z axis. This would be the line to add inside of FixedUpdate():

rigidbody.AddForce(Vector3.forward*z);

This will move forward or backward.

Comment
Add comment · Show 7 · 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 arianloco · Jul 18, 2014 at 06:56 PM 0
Share

Thanks man! Is there anyway to make him move forward backward and left and right?

 @script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
  
 //public variables
  
 var maxAirJumpCount : int = 1; // 1 for a double jump
  
 var normalJumpForce : float = 8; //strength of the normal jump's impulse
 var airJumpForce : float = 8; //strength of the double jump's impulse
  
 var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
  
 var xSpeed : float = 5; // horizontal speed of the character\
  
 var zSpeed : float = 5; //zspeed
  
 /* These values are all ideal settings that worked for me */
  
 //private variables
  
 private var airJumpCount : int = 0; //how many more times can the player jump?
  
 private var x$$anonymous$$ove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
                             // horizontal speed modifier (xSpeed)
  
 private var isGrounded : boolean = false; //is the player on the ground?
  
 function Update() {
  
     if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
  
         isGrounded = true;
         airJumpCount = maxAirJumpCount;
     }
     else {
  
         isGrounded = false;
     }
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
  
         if(isGrounded) {
             JumpNormal();
         }
         else if(airJumpCount > 0) {
  
             airJumpCount -= 1;
             JumpInAir();
         }
     }
  
     x$$anonymous$$ove = Input.GetAxisRaw("Vertical") * xSpeed;
  
         z = Input.GetAxisRaw("Horizontal") * zSpeed; 
  
             }
  
 function FixedUpdate () {
  rigidbody.AddForce(Vector3.right*x$$anonymous$$ove);
     rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);
 }
  
  
 function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, Force$$anonymous$$ode.Impulse); }
  
 function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, Force$$anonymous$$ode.Impulse); }


avatar image Fearthefrozt · Jul 18, 2014 at 07:40 PM 0
Share

Ah sorry I made a mistake in my response, it should be:

rigidbody.AddForce(Vector3.forward*z);

switch out the x$$anonymous$$ove in the second line to the z variable that's taking the horizontal axis, this should allow for movement all around. You might want to switch the x$$anonymous$$ove and z variables depending on which keys you want to move side to side vs forward, I'll edit my answer to reflect this.

avatar image arianloco · Jul 19, 2014 at 02:04 AM 0
Share

I want Z to control the horizontal movement while X is the vertical movement. Picture the game like a first person platformer. You would move Z with "A" and "D", and move X with "W" and "S". I tried adding your code, is this right? Because I seem to be getting an error saying "$$anonymous$$ Identifier 'Z'" Your help has been great man btw :D

 @script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
  
 //public variables
  
 var maxAirJumpCount : int = 1; // 1 for a double jump
  
 var normalJumpForce : float = 8; //strength of the normal jump's impulse
 var airJumpForce : float = 8; //strength of the double jump's impulse
  
 var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
  
 var xSpeed : float = 5; // horizontal speed of the character\
  
 var zSpeed : float = 5; //zspeed
  
 /* These values are all ideal settings that worked for me */
  
 //private variables
  
 private var airJumpCount : int = 0; //how many more times can the player jump?
  
 private var x$$anonymous$$ove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
                             // horizontal speed modifier (xSpeed)
  
 private var isGrounded : boolean = false; //is the player on the ground?
  
 function Update() {
  
     if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
  
         isGrounded = true;
         airJumpCount = maxAirJumpCount;
     }
     else {
  
         isGrounded = false;
     }
  
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
  
         if(isGrounded) {
             JumpNormal();
         }
         else if(airJumpCount > 0) {
  
             airJumpCount -= 1;
             JumpInAir();
         }
     }
  
     x$$anonymous$$ove = Input.GetAxisRaw("Vertical") * xSpeed;
  
         z = Input.GetAxisRaw("Horizontal") * zSpeed; 
  
             }
  
 function FixedUpdate () {
  rigidbody.AddForce(Vector3.forward*z);
     rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);
 }
  
  
 function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, Force$$anonymous$$ode.Impulse); }
  
 function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, Force$$anonymous$$ode.Impulse); }
avatar image sbroscious · Jul 19, 2014 at 02:56 AM 0
Share
 function FixedUpdate () {
  rigidbody.AddForce(Vector3.forward*z);
     rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);

Shouldn't this be:

  function FixedUpdate () {
      rigidbody.AddForce(Vector3.forward*z);
         rigidbody.AddForce(Vector3.right*x$$anonymous$$ove);//the difference can be found in this argument


I think changing this up will provide the desired outcome described when you say, "I want Z to control the horizontal movement while X is the vertical movement." Your current implementation is basically saying that for either Input.GetAxisRaw("Vertical") or Input.GetAxisRaw("Horizontal") move forward by speeds z or x$$anonymous$$ove...if I understand it right (I know 0 java)

avatar image arianloco · Jul 19, 2014 at 03:39 AM 0
Share

I can't figuere out this damn Z error. D:

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

My character keep walking left. (bug) 0 Answers

JavaScript: Animation on button Help. 1 Answer

JS: Access sections of an array, without activating the rest 2 Answers

Entering Data into a 2d array overwrite issue. 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