I´m making a top down scrolling space asteroids shooter and so i tried to use a slider to control the movement of my spaceship from the left to the right, but it doesn´t work.(Spaceship doesn’t move when changing the slider)
Here’s my code i hope someone can help me. (And i assigned the onvalueChange)
Playermovement code:
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
public void OnSliderChanged(float value)
{
if(value < 0)
{
Moveleft();
}
if(value > 0)
{
MoveRight();
}
}
private void Moveleft()
{
rb.velocity = new Vector2(-5, 0f);
Debug.Log("1");
}
private void MoveRight()
{
rb.velocity = new Vector2(5, 0f);
}
Here the Slider Code in Case you need it:
public Slider slider;
private float StartPosition = 0;
// Start is called before the first frame update
void Start()
{
slider = GetComponent <Slider>();
slider.value = StartPosition;
}
private void ResetSlider()
{
slider.value = StartPosition;
}
public void OnPointerUp(PointerEventData eventData)
{
ResetSlider();
}
Here’s how you can help yourself by actually finding out what your code is doing:
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
If your problem would benefit from in-scene or in-game visualization, consider using Debug.DrawRay() or Debug.DrawLine() to visualize things like raycasts or distances.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Firstly thanks for the fast replys. What i didn’t mentioned, that i got an NullReferenceException about my ridgidbody in both Move methods, although I iniate it in the start method. Second I’m sorry, i used the debug and found out that the value is passed on but both Move methods just don`t affect my Gameobject, although how shown in the code i get my debug(1) on the MoveLeft Funktion. The min of my slider is -1 and the max is 1. I hooked the onlsiderchanged with my players gameobject(where my movementcontroller is[first code]) and with the first Onsliderchanged without the float at the end. Hope you can understand my problem so better.
Yeah i tried and fixed it, kind of: i just copyied rb = GetComponent(); into my move methods and the error disapperard from my consol, but it didn’t changed my results. And even after i deleted it again the error was gone, although it was like before??