After I build and run my game, it is different when I see in the editor (after pressing the play button).
Everything in the editor go fine, however my character cannot run and jump in the standalone version.
I faced this problem before in editor version during development, therefore I believe that Unity has used the old version script.
I tried to reimport everything before build, but the problem can’t be solved.
I also tried to clean everything in myProject/Library/cache, but this even makes it worsen.(missing all materials )
Anyway its a pretty bad idea too clean the cache under any circumstances if that was a large project you would be hosed.
Also have no explanation why you are having issues with character movement in standalone nor have you provided sufficient details such as is this 3rd person, 1st person, 2d side scroll.
Maybe you could take a screen shot and post it here in this thread Highlighting the character so we can see what scripts are used in the inspector.
Things i would check: Did you import the Character Controller package? You could be using someone else s project.
My 1st Person Character in my game has these things:
I haven’t attached my script because I believe that the problem isn’t related to the script.
The main point is I had this issue in editor during my development so I don’t think the script is the main reason. I believe Unity has used the old version script for compile.
Anyway, I wrote more about my case:
animation is perfect in both cases but the character cannot move in standalone version.
I am using RigidBody + custom script for character movement, and use 3rd person camera.
My character keeps moving backward (in standalone), whatever I press any button. (but in editor, the character moves prerfectly.)
I have imported the Character Controller package but never use it.
I have tried to combine someone scripts and made some changes on that.
Here is the movement script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(AnimationHandler))]
public class AdvancedMovement : MonoBehaviour {
public static AdvancedMovement Instance;
public enum State {
Idle, Init, Setup, Run
}
public enum Turn { //enum for rotating character
left = -1,
none = 0,
right = 1
}
public enum Forward { //enum for moving character
back = -1,
none = 0,
forward = 1
}
public Transform characterTransform;
public Rigidbody characterRigid;
//moving forwards and backwards
public float walkSpeed;
public float runMultiplier = 2;
public float strafeSpeed;
public float AerialLerpModifier = 2;
//moving left and right, turning left and right
public float rotateAngle = 0.05f;
public Vector3 GravityDrop = new Vector3(0, -2f, 0);
public float MaximumGravity = 10;
public bool IsGrounded;
public float ConsideredFloor = 0.05f;
private RaycastHit _hit;
public float JumpHeight = 3;
public Vector3 Velocity;
//on the off hand we need to retain movment (air)
public Vector3 _currentMovement;
public Vector3 _currentGravity;
private bool _hasJumped;
private Vector3 _position;
private float airVelocity;
private Turn _turn;
private Forward _forward;
private Turn _strafe;
private bool _run = false;
private bool _jump = false;
private bool _fall = false;
private State _state;
public void Awake(){
characterTransform = transform;
characterRigid = rigidbody;
characterRigid.freezeRotation = true;
characterRigid.useGravity = false;
_state = AdvancedMovement.State.Init;
}
// Use this for initialization
IEnumerator Start () {
while(true){
switch(_state){
case State.Init:
Init();
break;
case State.Setup:
Setup();
break;
case State.Run:
ActionPicker();
break;
}
yield return null;
}
}
void OnCollisionStay(Collision other)
{
//Debug.Log("collide");
foreach (ContactPoint contact in other.contacts)
{
if (contact.normal.y > ConsideredFloor) continue;
IsGrounded = true;
airVelocity = 0;
if(Physics.Raycast(characterTransform.position, -Vector3.up, out _hit, 0.5f)){
IsGrounded = true;
airVelocity = 0;
}
}
}
void OnCollisionExit(Collision other)
{
if (other.collider.name == "Terrain")
IsGrounded = false;
}
private void Init(){
_state = AdvancedMovement.State.Setup;
}
private void Setup(){
_currentMovement = Vector3.zero;
animation.Stop(); //Stop all the animation and called when it is wanted.
_turn = AdvancedMovement.Turn.none;
_forward = AdvancedMovement.Forward.none;
_strafe = Turn.none;
_run = false;
_jump = false;
_fall = false;
_state = AdvancedMovement.State.Run;
}
private void ActionPicker(){
if(gameObject.tag == "Player" !_run){
PlayerCharacter player = (PlayerCharacter)gameObject.GetComponent<PlayerCharacter>();
player.AddjustBoosterValue(false);
}
//Left right rotation
Quaternion currentRotation = characterTransform.rotation;
characterTransform.RotateAround(Vector3.up, (float)((int)_turn * rotateAngle * 1.5));
_currentMovement = Vector3.zero;
//forward or backward
if(_forward != Forward.none){
if(_forward == Forward.forward){
_currentMovement += currentRotation * Vector3.forward;
_currentMovement = _currentMovement.normalized * walkSpeed;
if(_run){
if(gameObject.tag == "Player"){
PlayerCharacter player = (PlayerCharacter)gameObject.GetComponent<PlayerCharacter>();
if(player.curbooster > 0){
player.AddjustBoosterValue(true);
_currentMovement *= runMultiplier;
if(!_jump)
SendMessage("PlayRunBoosterAnim",SendMessageOptions.DontRequireReceiver);
}else{
SendMessage("PlayRunAnim",SendMessageOptions.DontRequireReceiver);
}
}
}else{
if(!_jump)
SendMessage("PlayRunAnim",SendMessageOptions.DontRequireReceiver);
}
}
else{
_currentMovement += currentRotation * Vector3.back;
_currentMovement = _currentMovement.normalized * walkSpeed;
SendMessage("PlayRunAnim",SendMessageOptions.DontRequireReceiver); //changed to Back();
}
}
//left or right
else if(_strafe != AdvancedMovement.Turn.none){
if(_strafe == AdvancedMovement.Turn.left){
_currentMovement += currentRotation * Vector3.left;
SendMessage("PlayStrafeLeftAnim",SendMessageOptions.DontRequireReceiver);
}else{
_currentMovement += currentRotation * Vector3.right;
SendMessage("PlayStrafeRightAnim",SendMessageOptions.DontRequireReceiver);
}
_currentMovement.x *= strafeSpeed;
}
else{
_currentMovement = Vector3.zero;
SendMessage("PlayIdleAnim",SendMessageOptions.DontRequireReceiver);
}
//if in air, we have little control over overall direction
if (!IsGrounded){
_currentMovement = Vector3.Lerp(characterRigid.velocity, _currentMovement, Time.deltaTime * AerialLerpModifier);
}
//if jumping
if (_jump){
if (IsGrounded || !IsGrounded){
_hasJumped = true;
}
}
if(IsGrounded){
_currentGravity = Vector3.zero;
}else{
_currentGravity += GravityDrop;
//SendMessage("PlayFallAnim",SendMessageOptions.DontRequireReceiver);
//Debug.Log("Fall?");
}
}
public void FixedUpdate()
{
float airVelocity = Mathf.Lerp(characterRigid.velocity.y, _currentGravity.y, Time.deltaTime);
if (_hasJumped){
_hasJumped = false;
_jump = false;
airVelocity += JumpHeight;
SendMessage("PlayJumpAnim",SendMessageOptions.DontRequireReceiver);
}
if (_currentMovement.y < -MaximumGravity){
_currentMovement.y = -MaximumGravity;
}
characterRigid.velocity = new Vector3(_currentMovement.x, airVelocity, _currentMovement.z);
Velocity = characterRigid.velocity;
}
public void MoveMeForward(Forward z){
_forward = z;
}
public void ToggleRun(){
_run = !_run;
}
public void RotateMe(Turn y){
_turn = y;
}
public void StrafeMe(Turn x){
_strafe = x;
}
public void JumpUp(){
if(IsGrounded)
_jump = true;
}
}