Objects interfering with eachother?

Hello everybody.

I’m somewhat new to Unity and got a problem with controlling my player character. My player character is two sides of a ball, left and right. They should be able to move independently from each other while jumping, but “rolling” should be together. “Rolling” is no problem, but the jumping is. I wanted to avoid using physics, therfore my gameobjects both got a rigidbody attached, gravity = false, is kinematic = true.

Now I wanted to control them with one single script instead of two, one for each side of the ball. I got the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

	public GameObject left;
	public GameObject right;

	public bool jumpRightInProgress;
	public bool jumpLeftInProgress;

	private float jumpSpeed = 0.45f;
	private float yVelocity;
	private float moveSpeedMultiplier = 8;

	private bool doJumpRight = false;
	private bool doJumpLeft = false;

	void Start () {
		jumpLeftInProgress = false;
		jumpRightInProgress = false;
	}
	
	void FixedUpdate () {
		float moveVertical = Input.GetAxis("Vertical");

		if (Input.GetKey(KeyCode.D) && right.transform.position.y == 1.0f && !jumpRightInProgress) {
			jumpRightInProgress = true;
			doJumpRight = true;
		}

		if (jumpRightInProgress) {
			JumpRight(moveVertical);
		}

		if (Input.GetKey(KeyCode.A) && left.transform.position.y == 1.0f && !jumpLeftInProgress) {
			jumpLeftInProgress = true;
			doJumpLeft = true;
		}

		if (jumpLeftInProgress) {
			JumpLeft(moveVertical);
		}
	}

	private void JumpRight(float moveVertical) {
		if (jumpRightInProgress) {
			yVelocity = yVelocity - Time.deltaTime;
			float finalVelocity = yVelocity;

			if (doJumpRight) {
				right.GetComponent<Rigidbody>().MovePosition(new Vector3(right.transform.position.x, right.transform.position.y + finalVelocity, right.transform.position.z + (moveVertical * moveSpeedMultiplier * Time.deltaTime)));
			}

			if (right.transform.position.y < 1.0f) {
				doJumpRight = false;
				jumpRightInProgress = false;
				right.transform.position = new Vector3(right.transform.position.x, 1.0f, right.transform.position.z + (moveVertical * moveSpeedMultiplier * Time.deltaTime));
				yVelocity = jumpSpeed;
			}
		}
	}

	private void JumpLeft(float moveVertical) {
		if (jumpLeftInProgress) {
			yVelocity = yVelocity - Time.deltaTime;
			float finalVelocity = yVelocity;

			if (doJumpLeft) {
				left.GetComponent<Rigidbody>().MovePosition(new Vector3(left.transform.position.x, left.transform.position.y + finalVelocity, left.transform.position.z + (moveVertical * moveSpeedMultiplier * Time.deltaTime)));
			}

			if (left.transform.position.y < 1.0f) {
				doJumpLeft = false;
				jumpLeftInProgress = false;
				left.transform.position = new Vector3(left.transform.position.x, 1.0f, left.transform.position.z + (moveVertical * moveSpeedMultiplier * Time.deltaTime));
				yVelocity = jumpSpeed;
			}
		}
	}
}

In short:

When the object is grounded (y == 1.0f) and no jump is in progress, then call Jump (e.g. right) every fixed update.

In the jump function we’re calculating a velocity. 0.45f minus Time.deltaTime and add this to the objects y-axis. The velocity will become smaller by time, so the object well go up until some point and come back down. If the y-axis is below 1.0f, all the bools get deactivated and the object is getting re-setted to y = 1.0f.

This works pretty well for each of the objects (right and left). But when I hit both jump keys simultaneously (A +D at nearly the same time) both of the objects jump only like half the distance, as they do when just one is jumping.

But this isn’t my onl y problem. When I jump first with the right object (hitting D) and whie is is airborne and hit A for the other object to jump, the first one (right) will jump again. Like it’s ground is getting reset to the current y position. It’s hard to describe in english, sorry.

I couldn’t find a logical error in my code above. I made sure to deactivate all objects not needed for this script to run. I’m out of ideas. If anybody got an idea, I’d appreciate that.

Kind regards

Nico

Nevermind. I found the error myself.
yVelociy was used in both jumping functions. Therefore they were manipulating this variable both, so everything went wonky. Thanks anyway.