- Home /
Sphere fly's when I want it to jump
Hey guys so I am trying to make a sphere jump. Also before you say anything I have only been using unity for 3 days........ So I have this code but I can't jump. I have searched for about 2 hours but couldn't find anything to work. I have developed some apps in java but want to get into gaming. I have not learnt c# and I am basing everything off what I have learnt in java. Btw I could have used switch statements but do not want to use them. I had some code before which I was able to jump but it would only jump backwards
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rbS;
public float movementSpeed;
private bool jump;
// Time.deltaTime makes everything frame rate independent
private void Start()
{
rbS = GetComponent<Rigidbody>();
}
void FixedUpdate () {
if (Input.GetKey("a")) // This allows you to move left
{
rbS.AddForce(-movementSpeed * Time.deltaTime, 0, 0); // Adding a force on the x axis
}
else if (Input.GetKey("d")) // This allows you to move right
{
rbS.AddForce(movementSpeed * Time.deltaTime, 0, 0); // Adding a force on the x axis
}
else if (Input.GetKey(KeyCode.Space)) // This is supposed to make you jump
{
rbS.AddForce(0, movementSpeed * Time.deltaTime, 0); // Adding a force on the y axis
}
else if (Input.GetKey("w")) // This is supposed to make you go forward
{
rbS.AddForce(0, 0, movementSpeed * Time.deltaTime); // Adding a force on the z axis
}
else if (Input.GetKey("s")) // This is supposed to make you go back
{
rbS.AddForce(0, 0, -movementSpeed * Time.deltaTime); // Adding a force on the z axis
}
}
}
Have you tried debugging your code? When you add breakpoints or log statements, does the rbS.AddForce line for jumping get called? Is movementSpeed big enough? Have you tried different settings? What are the settings on the rigidbody? Is it maybe set to kinematic?
well it can jump but if i hold my spacebar it just keeps on going
@sleepandpancakes is right and put the input code in Update() so you don't miss an input
Answer by sleepandpancakes · Apr 16, 2017 at 05:46 AM
Change Input.GetKey(KeyCode.Space)
to Input.GetKeyDown(KeyCode.Space)
so that it only adds the force once when you press the button, rather than adding force the entire time the space button is pressed. You probably also want to specify the ForceMode as Impulse, i.e. rbS.AddForce(0, forceValue, 0, ForceMode.Impulse);
Yea its still not jumping. I have my mass set to 35, gravity is on, drag is 0 and angular drag is 0.05.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public Rigidbody rbS;
public float movementSpeed;
private bool jump;
// Time.deltaTime makes everything frame rate independent
private void Start()
{
rbS = GetComponent<Rigidbody>();
}
void FixedUpdate () {
if (Input.Get$$anonymous$$ey("a")) // This allows you to move left
{
rbS.AddForce(-movementSpeed * Time.deltaTime, 0, 0, Force$$anonymous$$ode.Impulse); // Adding a force on the x axis
}
else if (Input.Get$$anonymous$$ey("d")) // This allows you to move right
{
rbS.AddForce(movementSpeed * Time.deltaTime, 0, 0, Force$$anonymous$$ode.Impulse); // Adding a force on the x axis
}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) // This is supposed to make you jump
{
rbS.AddForce(0, movementSpeed * Time.deltaTime, 0, Force$$anonymous$$ode.Impulse); // Adding a force on the y axis
}
else if (Input.Get$$anonymous$$ey("w")) // This is supposed to make you go forward
{
rbS.AddForce(0, 0, movementSpeed * Time.deltaTime, Force$$anonymous$$ode.Impulse); // Adding a force on the z axis
}
else if (Input.Get$$anonymous$$ey("s")) // This is supposed to make you go back
{
rbS.AddForce(0, 0, -movementSpeed * Time.deltaTime, Force$$anonymous$$ode.Impulse); // Adding a force on the z axis
}
}
}
Since Time.deltaTime
is a small value, you are adding very little force to the sphere. Change movementSpeed * Time.deltaTime
to a different float forceValue
and increase it. Or you might want to use a VelocityChange Force$$anonymous$$ode ins$$anonymous$$d of an Impulse. Read more about them here