Beginner here, going thru tutorials, so excuse if the question is a bit stupid. Variable names are in Finnish, i don’t like to copy them. I feel like i get things better in my head if i change a bit, and variable names are safe to change.
When i run this script, the player-object just jumps and and falls down. What i’m missing here, i couldn’t pinpoint the faulty bit?
public class Pelaajakontrolli : MonoBehaviour {
public float vauhti;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
float Vertical = Input.GetAxis ("Vertical");
float Horizontal = Input.GetAxis ("Horizontal");
Vector3 liikkuminen = new Vector3 (Vertical, 0.0f, Horizontal);
rb.AddForce (liikkuminen * vauhti);
}
}
I don’t see anything in there that would make anything “jump”… is this the only script you’ve got?
Yes, Only one. When i click the play button, after a second my object just jumps on it’s own, turns in the air a bit and then falls flat.
I used a cube as an object, that i transformed a bit (shaped like a thin board), but that shouldn’t affect it.
could it be that the cube is slightly clipping through what you are using as the ground at the very start? so a physic engine interaction rather than a scripting thing.
Seemed to be that yes! Now i know to look it out in the future.
Other question: With that script, i can only make my cube fall to one side, or the other? What’s that about? Physics engine as well?
I made the ball to roll with a script like that, i thought i’d make the cube move as well.
Hi Black_star87,
I took your script and tested it out.
It worked great for me.
The veriable vauhti, I had to have it higher then 20 to get the force strong enough to roll the cube over.
change fixedUpdate() to just Update()
add this to your code to make it jump with the space bar
float Jump = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump = 15.0f;
}
Vector3 liikkuminen = new Vector3(Vertical, Jump, Horizontal);