- Home /
i want to move a ball on z axis and jump on y axis ,
i'am new to unity and want to make game where ball is moving and want to jump when we touch the platform on which it is moving. please help
this is the code which iam using
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallController : $$anonymous$$onoBehaviour { public float force = 100000f; public float jump =50000f; public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() { rb.AddForce(0, 0, Time.deltaTime * force);
}
private void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
rb.AddForce(0, jump * Time.deltaTime, 0) ;
}
}
}
Answer by hexagonius · Dec 22, 2018 at 10:42 AM
the are several Tutorials out there, you should check them out, especially this one:
https://unity3d.com/de/learn/tutorials/s/roll-ball-tutorial
i could not find tutorial helpful it just tells how to move the balls, can you please check my code and tell where iam wrong
Answer by GamitusLabs · Dec 22, 2018 at 03:50 PM
I'm not sure which AddForce you are using, this overload is the most basic: AddForce(Force, Mode)
Example: AddForce(Vector3.up * force, ForceMode.Impulse);
For a more in depth explanation on the modes: https://docs.unity3d.com/ScriptReference/ForceMode.html
A few other notes:
Your force values are insanely high and when you get it working you're probably going to see your object just disappear.
Apply your physics forces in FixedUpdate to keep the application of forces normalized.
Your answer
Follow this Question
Related Questions
Ball Rolling Game Help 0 Answers
move character controller with collision 3 Answers
how can I make the character jump more by holding the jump button in this script? 1 Answer
Character fall animation 1 Answer
How can i move a 2D ball ? 0 Answers