Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
This question was closed Jul 03, 2014 at 01:38 PM by ado112 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by ado112 · Jun 01, 2014 at 03:01 PM · inputcharactercontrollermoving

Why moveDirection.z doesn't work?

part of the code:

             if(Input.GetButtonDown("Fire1")  &&  controller.isGrounded){
             playIdle = false;
             animation.CrossFade("CHit1",0.01);
             animation["CHit1"].speed = 0.23;
             moveDirection.z = 20;
             moveDirection = transform.TransformDirection(moveDirection);
             }
Comment
Add comment · Show 5
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 Jeff-Kesselman · Jun 01, 2014 at 03:02 PM 0
Share

What mode are you in? 2D or 3D? What do you means by "doesn't work"? Please be more specific as to what you a re trying to accomplish and what is happening.

By definition, in 2D, Z movement means nothing.

avatar image ado112 · Jun 01, 2014 at 03:04 PM 0
Share

3D,when i click left mouse button it wont move.

avatar image Jeff-Kesselman · Jun 01, 2014 at 03:27 PM 0
Share

Well since moveDirection does nothing, Im not surprised.

The code you gave us has no movement code. if there is more code that does, then show it to us.

As is I have no idea even how moveDirection is defined, let alone how it issued.

avatar image ado112 · Jun 01, 2014 at 04:19 PM 0
Share

code:

     var gravity : float = 20.0;
     var AirSpeedRemove : float = 2.0;
     
     private var moveDirection : Vector3 = Vector3.zero;
     private var secondJumped = false;
     private var playIdle = true;
     
     function Update() {
     
         var controller : CharacterController = GetComponent(CharacterController);
         
         transform.Rotate(0,Input.GetAxis("Horizontal"),0);
         
             if(Input.GetButtonDown("Fire1")  &&  controller.isGrounded){
             playIdle = false;
             animation.CrossFade("CHit1",0.01);
             animation["CHit1"].speed = 0.23;
             moveDirection.z = 20;
             moveDirection = transform.TransformDirection(moveDirection);
             }
             
         if (controller.isGrounded) {
             if(playIdle == true){
             animation.CrossFade("Idle");
             }
             secondJumped = false;
             moveDirection = Vector3(0, 0,Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
                 animation.CrossFade("Jump1");
             }
         }
         else {
     moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection.z *= speed/AirSpeedRemove;
     moveDirection.x *= speed/AirSpeedRemove;
 }
 
            
          
                 if(Input.GetButtonDown("Jump") && controller.isGrounded == false && secondJumped == false){
                 moveDirection.y = secondJumpSpeed;
                 secondJumped = true;
                 animation.CrossFade("Jump2");
                 }
                 
                 if(Input.GetAxis("Vertical") && controller.isGrounded){
                 animation.CrossFade("Run");
                 if(Input.GetButtonDown("Jump")){
                 animation.CrossFade("Jump1");
                 } 
                 }
 
         moveDirection.y -= gravity * Time.deltaTime;
         
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }
avatar image Jeff-Kesselman · Jun 01, 2014 at 05:31 PM 0
Share

I don't see speed defined and set anywhere. Thus, by this script, it is 0, which means all your functions using speed result in 0.

2 Replies

  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 01, 2014 at 05:33 PM

I don't see speed defined and set anywhere. Thus, by this script, it is 0, which means all your functions using speed result in 0.

Rather then just dropping your first attempt at code on others in the hopes that they will fix it, you should learn to properly debug your code.

Debugging is a matter of checking each value along the way (usually through Debug.Log) to see fi it is what yo expect it to be and, when you find the first one that isn't, ascertaining why,

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 ado112 · Jun 01, 2014 at 05:41 PM 0
Share

$$anonymous$$y code is correct,speed is in script but it wasn't pasted in code,only moveDirection.z = 20 is not working and i don't know why?

      var speed : float = 6.0;
     var jumpSpeed : float = 8.0;
     var secondJumpSpeed : float = 15.0;
     var gravity : float = 20.0;
     var En1 : EnemySph1;
     var AirSpeedRemove : float = 2.0;
     
     private var moveDirection : Vector3 = Vector3.zero;
     private var secondJumped = false;
     private var playIdle = true;
     
     function Update() {
     
         var controller : CharacterController = GetComponent(CharacterController);
         
         transform.Rotate(0,Input.GetAxis("Horizontal"),0);
         
             if(Input.GetButtonDown("Fire1")  &&  controller.isGrounded){
             playIdle = false;
             animation.CrossFade("CHit1",0.01);
             animation["CHit1"].speed = 0.23;
             moveDirection.z = 20;
             moveDirection = transform.TransformDirection(moveDirection);
             }
             
         if (controller.isGrounded) {
             if(playIdle == true){
             animation.CrossFade("Idle");
             }
             secondJumped = false;
             moveDirection = Vector3(0, 0,Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
                 animation.CrossFade("Jump1");
             }
         }
         else {
     moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection.z *= speed/AirSpeedRemove;
     moveDirection.x *= speed/AirSpeedRemove;
 }
 
            
          
                 if(Input.GetButtonDown("Jump") && controller.isGrounded == false && secondJumped == false){
                 moveDirection.y = secondJumpSpeed;
                 secondJumped = true;
                 animation.CrossFade("Jump2");
                 }
                 
                 if(Input.GetAxis("Vertical") && controller.isGrounded){
                 animation.CrossFade("Run");
                 if(Input.GetButtonDown("Jump")){
                 animation.CrossFade("Jump1");
                 } 
                 }
 
         moveDirection.y -= gravity * Time.deltaTime;
         
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }
avatar image
0

Answer by gamertest · Jun 24, 2014 at 04:36 AM

if(Input.GetButtonDown("Fire1") && controller.isGrounded){ playIdle = true; animation.CrossFade("CHit1",0.01); animation["CHit1"].speed = 0.23; moveDirection.z = 20; moveDirection = transform.TransformDirection(moveDirection); }

Presuming you have that in a Vector3:

var localVector = transform.InverseTransformDirection(moveDirection); You have to change the playidle flase to true just little change make it worked

Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game link, just use the z axis

// Just use the z axis transform.LookAt(transform.position + Vector3(0,0,moveDirection.z));

// Or set the y axis to 0 var tempDirection = moveDirection; tempDirection.y = 0; transform.LookAt(transform.position + moveDirection);

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 EmmaTVanhook · Apr 03 at 10:32 AM 0
Share
  moveDirection = Vector3(0, moveDirection.y, Input.GetAxis("Vertical"));
  moveDirection = transform.TransformDirection(moveDirection);
  moveDirection.z *= speed/AirSpeedRemove;
  moveDirection.x *= speed/AirSpeedRemove;

Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game link, just use the z axis

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why am I losing some inputs on Update function? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Input.GetAxis won't start working properly. 1 Answer

(C#) A better way to limit actions to once per button press? 2 Answers

Can you access non-standard mouse buttons? 2 Answers


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