M106
M106

Reputation: 17

Script in unity won't change

I made a script that uses .AddForce to move a ball. when i use it on the z axis, it works fine, but when i move on the x axis, it goes way too fast. The speed used to be 300 for the x axis, but i changed it to 50 for both. force on the x axis is still too high, and it won't change.

using UnityEngine;
public class ball_movement : MonoBehaviour
{

    public Rigidbody ballmovement;
    public float zforce = 50f;
    public float xforce = 50f;

    private void FixedUpdate()
    {
        if (Input.GetKey("w"))
        {
            //ball moves right
            ballmovement.AddForce(0, 0, zforce * Time.deltaTime, 
ForceMode.VelocityChange);
        }
         if (Input.GetKey("s"))
         {
            //ball moves left
            ballmovement.AddForce(0, 0, -zforce * Time.deltaTime, 
ForceMode.VelocityChange);
        }
        if (Input.GetKey("d"))
        {
            //ball moves forward
            ballmovement.AddForce(xforce, 0, 0 * Time.deltaTime, 
ForceMode.VelocityChange);
        }
        if (Input.GetKey("a"))
        {
            //ball moves right
            ballmovement.AddForce(-xforce, 0, 0 * Time.deltaTime, 
ForceMode.VelocityChange);
        }
    }
}

What am I doing wrong?

Upvotes: 0

Views: 40

Answers (1)

Walter Palladino
Walter Palladino

Reputation: 479

Check that you are not affecting * Time.deltaTime for the xforce. Hope it helps

Upvotes: 2

Related Questions