- Home /
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):
Blu (Second cat, undesired behavior, jump input is highlighted)
Have you got the same value of gravity for each of them in the inspector?
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.
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.)
both have a characterController attached, any difference in values there?
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.
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.
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!
Nope. Values in the inspector are as they appear here, on both scripts: 6.0f, 8.0f, 20.0f
hmmm. apart from different inputs, they do look identical.
have you defined the axis/buttons for the second cat?
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...!)
Your answer
Follow this Question
Related Questions
controller.Move not working correctly 0 Answers
My Player can't jump - coding 1 Answer
Problem CC and plane 0 Answers