using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moveball : MonoBehaviour
{
Rigidbody rb;
public int ballspeed = 0;
public int jumpspeed = 0;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float Hmove = Input.GetAxis ("Horizontal");
float Vmove = Input.GetAxis ("Vertical");
Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
rb.AddForce (ballmove * ballspeed);
if(Input.GetKey(KeyCode.Space));
{
Vector3 balljump = new Vector3 (0.0f, 0.0f, 0.0f);
rb.AddForce(balljump * ballspeed);
}
}
}
is there anything wrong with any of the code for the ball to jump if there is anything wrong with the code for jumping please edit it so I can see what I did wrong with my code and
I don’t understand what you’re doing on lines 27 & 28. With balljump being (0f,0f,0f) then the math on line 28 will always result in a force of 0f, so why even try adding a 0f force? 0 times any number is always 0 of course.
Don’t add forces in Update. Add them in FixedUpdate. But keep your input checking in Update.
I’m trying to get the ball to jump which is not working could you suggest a solution to this problem I’m having with it not jumping and possibly write some code to make the object jump
Well here you are setting ballJump to zero on xyz. So when you AddForce the calculation looks like
Force is now Zero * BallSpeed. So instead i’d put Vector3 balljump = new Vector3 (0.0f, 1.0f, 0.0f); to say i’d want it to go up in the Y direction.
Also I assume you set ballSpeed and jumpSpeed to something other then zero in your inspector? I’d probably change those lines to
public int ballspeed = 1;
public int jumpspeed = 1;
Just to make sure the default value still does something / shows the behavior of the script.
if(Input.GetKey(KeyCode.Space));
I’d also change this to a Input.GetKeyDown so it triggers once but note that you’ll want to up the ball jump force by a ton since it will only trigger on one frame.
Lastly I’d restructure this a bit since your not supposed to add force in Update. Personally the exception I make to this is for one off forces like a jump. So the final version would look something like this
EDIT: Updated Version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallMove : MonoBehaviour
{
Rigidbody rb;
public int ballspeed = 10;
public int jumpspeed = 10;
private float Hmove;
private float Vmove;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Hmove = Input.GetAxis("Horizontal");
Vmove = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 balljump = new Vector3(0.0f, 100.0f, 0.0f);
rb.AddForce(balljump * jumpspeed);
}
}
void FixedUpdate()
{
Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
rb.AddForce(ballmove * ballspeed);
}
}
Also if you interested in learning more about C# i’d recommend this tutorial. Once you know the rules of programming things become much easier because you can work things out.
If you do have a background in programming then I’d look over the unity docs to understand when and how these built in functions are called and how they should be used.
Sorry about that. The reason why the code errored out was that Hmove and Vmove were created in the scope of the update function/method. I’ve moved them above so all of them have access to it.
As for it just floating up GetKeyDown is what you want since that will trigger once. Not GetKey.
Updated Version:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallMove : MonoBehaviour
{
Rigidbody rb;
public int ballspeed = 10;
public int jumpspeed = 10;
private float Hmove;
private float Vmove;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Hmove = Input.GetAxis("Horizontal");
Vmove = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 balljump = new Vector3(0.0f, 100.0f, 0.0f);
rb.AddForce(balljump * jumpspeed);
}
}
void FixedUpdate()
{
Vector3 ballmove = new Vector3(Hmove, 0.0f, Vmove);
rb.AddForce(ballmove * ballspeed);
}
}
I would recommend using Vector3.up * jumpSpeed instead of defining an arbitrary upwards vector like (0,100,0).
Also, for jumping i would recommend using AddForce(yourForce, ForceMode.Impulse) instead. ForceMode.Impulse is used to apply forces that are supposed to happen more or less instantly, as contrary to happening over time.