- Home /
My character won't move
I am very new to Unity and C# and I was following a simple tutorial (2-3 years old) I used the exact same code as they did but the player (a ball) won't even move left and right. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddPlayerControlledVelocity : MonoBehaviour
{
[SerializeField]
Vector3 v3Force;
[SerializeField]
KeyCode keypositive;
[SerializeField]
KeyCode keynegative;
void FixedUpdate()
{
if (Input.GetKey(keypositive))
GetComponent<Rigidbody>().velocity += v3Force;
if (Input.GetKey(keypositive))
GetComponent<Rigidbody>().velocity -= v3Force;
}
Answer by Axel_BIaze · Feb 07, 2021 at 11:33 AM
At first glance it seems like you have accidentally written out the following code twice: if (Input.GetKey(keypositive)) GetComponent().velocity += v3Force;
as a result of this, when you press the key set as positive nothing happens as they cancel each other out while pressing the negative key also does nothing as it isn't scripted to do anything.
Just change the second one of the code mentioned above to: if(Input.GetKey(keynegative)) It should work! Hope that helps! ^-^
Your answer
Follow this Question
Related Questions
Newbie question 2 Answers
What type of code should I learn for a new game? 3 Answers
ArgumentException: Input Axis Axis.HORIZONTAL is not setup? 2 Answers
Jump Script Not working (3D) 2 Answers
Add Force At Player 0 Answers