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 Reverend-Speed · Jan 19, 2015 at 07:46 PM · movementinputcharactercontrollerjumpmove

Two near-identical scripts cause a cat to jump in different ways - why?

The first cat repeatedly jumps a decent distance on input, the second makes small hops repeatedly when the button is held. I've stared at the code for ages, but can't see the critical difference. Can I get fresh eyes?

I'm using a version of the sample code found here to move my two cat characters around the environment. I'm in the process of separating the interface and movement code (good practice, I hear).

My original code (currently controlling the first cat) looks like this (this gives desired jumping behavior):

 using UnityEngine;
 using System.Collections;
 
 public class CatControlP1Red : MonoBehaviour {
     public    float         speed             = 6.0F;
     public    float         jumpSpeed         = 8.0F;
     public    float         gravity         = 20.0F;
     private Vector3     moveDirection     = Vector3.zero;
     public    Transform     catRotate;
 
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("L_XAxis_1"), 0, Input.GetAxis("L_YAxis_1"));
 
             moveDirection = transform.TransformDirection(moveDirection);
             if(moveDirection != Vector3.zero)Rotate (moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("A_1"))    moveDirection.y = jumpSpeed;    
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 
     void Rotate(Vector3 catTarget){
 
         Quaternion rotation = Quaternion.LookRotation(catTarget);
         catRotate.rotation = rotation;
 
     }
 }
 

...while the code controlling the second cat (two scripts, CatControlP2Blu and CatMove) looks like this(this gives undesired hopping behavior):

 using UnityEngine;
 using System.Collections;
 
 public class CatControlP2Blu : MonoBehaviour {
     private    CatMove        catMove;
 
     void Awake () {
         catMove                 = GetComponent<CatMove> ();
     }
 
     void Update() {
         Vector3 moveDirection     = new Vector3(Input.GetAxis("L_XAxis_2"), 0, Input.GetAxis("L_YAxis_2"));
         bool    jump            = Input.GetButton("A_2");
         catMove.MoveCat(moveDirection, jump);
     }
 }
 

...and this:

 using UnityEngine;
 using System.Collections;
 
 public class CatMove : MonoBehaviour {
 
     public     Transform     catRotate;
     public     float         speed             = 6.0F;
     public     float         jumpSpeed         = 8.0F;
     public     float         gravity         = 20.0F;
 
     public    void MoveCat (Vector3 moveDirection, bool jump) {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
 
 
             moveDirection = transform.TransformDirection(moveDirection);
             if(moveDirection != Vector3.zero)Rotate (moveDirection);
             moveDirection *= speed;
             if (jump)    moveDirection.y = jumpSpeed;
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 
     void Rotate(Vector3 catTarget){
         
         Quaternion rotation = Quaternion.LookRotation(catTarget);
         catRotate.rotation = rotation;
         
     }
 }

I've sat the two methodologies beside each other in-editor (Sublime) and I cannot see why the first jumps normally and the second barely hops for life of me.

What am I not seeing? Many thanks in advance.

--Rev

EDIT: A further description of the current observed behavior...

Using the code above, unchanged but for replacing GetButton with GetButtonDown (to eliminate hopping),

The entirety of Cat1's jump lasts approx. 0.73 seconds and takes it approx a meter and a half off the ground.

The entirety of Cat2's jump lasts approx. 0.28 seconds and takes it approx 15-20 cm off the ground.

FURTHER EDIT: Red (First Cat, desired behavior, jump input is highlighted): alt text

Blu (Second cat, undesired behavior, jump input is highlighted) alt text

Comment
Add comment · Show 6
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 sc0by · Jan 20, 2015 at 11:35 AM 0
Share

Have you got the same value of gravity for each of them in the inspector?

avatar image sc0by · Jan 20, 2015 at 11:49 AM 0
Share

It's a relief that no-one else can see a practical difference between the scripts, I suppose :/

Silly season: try swapping L_XAxis_1 and L_XAxis_2 by editing the scripts and see if there's any difference if you give it a different input.

avatar image Reverend-Speed · Jan 20, 2015 at 12:01 PM 0
Share

Both cats have the same level of gravity in the inspector and scripts - 20.

Swapped the axiseseses around, doesn't seem to have made any difference to the behavior (which is reasonable, as they were acting fine until I split the second script into two). Appreciate the idea, though! Cheers.

(Just had the suggestion that in the second cat, I am passing moveDirection as an argument, so it becomes a local variable in $$anonymous$$oveCat and thus losing it's value after every call to $$anonymous$$oveCat.

$$anonymous$$akes a certain amount of sense, just trying to work out how I test that.)

avatar image sc0by · Jan 20, 2015 at 12:04 PM 0
Share

both have a characterController attached, any difference in values there?

avatar image Reverend-Speed · Jan 20, 2015 at 12:05 PM 0
Share

Good thinking, but sadly they're identical.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gjf · Jan 20, 2015 at 12:07 PM

but you're setting it explicitly before the function call which is equivalent. it's not like you're relying on the value from previous frames...

EDIT: actually that's NOT true. if isGrounded isn't set then you ARE relying on the previous value...

EDIT2: what you need to do is have a local moveDirection and only modify it with the parameter passed in if isGrounded is set... can't believe i didn't spot that sooner.

Comment
Add comment · 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
0

Answer by SnStarr · Jan 20, 2015 at 11:02 AM

in the first snippet you designate moveDirection to something:

 moveDirection = new Vector3(Input.GetAxis("L_XAxis_1"), 0, Input.GetAxis("L_YAxis_1"));

In the third snippet of code you do not.

Comment
Add comment · Show 9 · 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 Reverend-Speed · Jan 20, 2015 at 11:21 AM 0
Share

Hi there! I might be misunderstanding you here - are you saying that I'm failing to assign a value to moveDirection in the third script?

I think I am - in the third script, moveDirection is a Vector3 created and then passed from the second script (see lines 12 and 14 in the second script) to the $$anonymous$$oveCat() function in the third.

Indeed, the second character moves around in a manner identical to the first (as handled by moveDirection) - the issue is that the jumping behavior differs between the first and second character!

Really appreciate the helping hand - if I've misunderstood you or you've another idea, please lemme know!

avatar image gjf · Jan 20, 2015 at 11:36 AM 0
Share

did you modify speed, jumpSpeed or gravity in the inspector?

avatar image Reverend-Speed · Jan 20, 2015 at 11:38 AM 0
Share

Nope. Values in the inspector are as they appear here, on both scripts: 6.0f, 8.0f, 20.0f

avatar image gjf · Jan 20, 2015 at 11:43 AM 0
Share

hmmm. apart from different inputs, they do look identical.

have you defined the axis/buttons for the second cat?

avatar image Reverend-Speed · Jan 20, 2015 at 12:56 PM 1
Share

Post that comment in an Answer, gjf, you done gotcherself a SOLUTION!Lemme vote that up. =)

(Now I just have to make the damn thing have air control and variable jump height... one thing at a time...!)

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

27 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 avatar image avatar image

Related Questions

controller.Move not working correctly 0 Answers

My Player can't jump - coding 1 Answer

Problem CC and plane 0 Answers

Rigidbody Jumping for Character Controller 1 Answer

Why am I losing some inputs on Update function? 0 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