- Home /
Movement Script Only Works in One Scene
This is the movement script I have attached to my player character prefab. In my test scene it works perfectly fine, but if I create another instance of the prefab in any other scene, the camera rotation still works, but I cannot move the character or jump at all. How can that be?
public class Move : MonoBehaviour
{
public float speed;
public float rotationSpeed;
public float jumpHght;
public float grav;
bool jumping;
bool moving;
public AudioClip jumpSound;
public AudioClip landSound;
private Rigidbody rb;
//Sets variables.
void Start()
{
Physics.gravity = new Vector3(0, grav, 0);
rb = GetComponent<Rigidbody>();
}
void Update()
{
//Jumps, if MagBot is not already in air.
if(Input.GetButtonDown("Jump") && jumping == false)
{
rb.AddForce(new Vector3(0, jumpHght, 0), ForceMode.Impulse);
GetComponent<AudioSource>().PlayOneShot(jumpSound);
}
}
//Determines when MagBot is not in air.
void OnCollisionStay(Collision collision)
{
jumping = false;
}
//Determines when MagBot is in air.
void OnCollisionExit(Collision collision)
{
if(!jumping)
{
jumping = true;
}
}
//Plays collision sound effects after landing from jump.
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Envir")
{
GetComponent<AudioSource>().PlayOneShot(landSound);
}
}
void FixedUpdate()
{
//Movement variables
float v = Input.GetAxisRaw("Vertical") * speed;
float h = Input.GetAxisRaw("Horizontal") * speed;
float r = Input.GetAxisRaw("Mouse X") * rotationSpeed;
v *= Time.deltaTime;
h *= Time.deltaTime;
r *= Time.deltaTime;
//Rotates MagBot/camera based on mouse movement.
transform.Rotate(0, r, 0);
//Moves MagBot based on WASD/Left analog stick.
rb.AddRelativeForce(0,0,-v, ForceMode.Impulse);
rb.AddRelativeForce(h,0,0, ForceMode.Impulse);
}
}
This doesn't answer your question, but I think you should multiply by Time.fixedDeltaTime in FixedUpdate() to get frame rate independent physics.
Answer by csgeorge · Feb 03, 2017 at 09:49 AM
Update: I've determined that this is somehow connected to the character's animator component: when it is on, the AddRelativeForce does not work. When the animator is unchecked, it works properly. This is however, still different from my test scene, in which the AddRelativeForce and animator both work together.
how did you uncheck the animator? my animations won't load and joystick won't work :(
Answer by FlyingHighUp · Feb 03, 2017 at 04:59 AM
It looks like things are okay, your issue is likely caused by something else holding your player still.
Unless... Does your rigidbody have constraints enabled on it? E.g. freezing position/rotation movements. Is the Time.deltaTime = 0? (e.g. if you set the timescale to 0 in your new scene) Are your variables set to 0? e.g. jumpHght, speed etc. Perhaps you didn't apply changes to the prefab?
The rigidbody has constraints freezing rotation, but that is as it should be, and is the same in the scene where it does work. Time is as it should be- everything except the AddRelativeForce is working correctly. All the variables are set to the proper numbers.