- Home /
Player's position won't change no matter what
I don't know why but one test, the player moves freely and fine and now it won't budge, even in scene view (this only happens when I'm playing in the Game View). I've tried everything I can think of and it's driving me insane.
Please Help.
I've finally figured it out. The problem was something to do with the animation
Answer by GameOverKid13 · Jun 27, 2019 at 06:45 AM
@faraz_zaidi22 using UnityEngine; using UnityEngine.UI;
public class PlayerControl : MonoBehaviour
{
public Rigidbody rb;
public float PlayerSpeed = 50f;
public bool Running = false;
public SpriteRenderer sr;
public Animation anim;
public Animator animator;
public bool canSideWalk = true;
public float switchdelay = 1;
public int character_id = 1;
public int encounter_chance = 1;
public PlayerControl playerControl;
public Battle battle;
public CameraControl cameraControl;
public GameObject player;
public bool canwalk = true;
void Start()
{
sr = GetComponent<SpriteRenderer>();
anim = GetComponent<Animation>();
animator = GetComponent<Animator>();
}
void FixedUpdate()
{
if (Input.GetKey("up"))
{
if (canwalk == true)
{
rb.AddForce(0, PlayerSpeed * Time.deltaTime, 0);
encounter_chance = Random.Range(1, 501);
if (character_id == 1)
{
canSideWalk = false;
animator.Play("CollinWalkUp");
Invoke("switchside", switchdelay);
}
if (encounter_chance == 69)
{
Debug.Log("Battle Started!");
if (battle.location != 0)
{
canwalk = false;
Invoke("StartBattle", 1.5f);
}
}
}
}
//same for all other directions
if (Input.GetKey("x"))
{
if (Running == false)
{
PlayerSpeed = PlayerSpeed * 2;
Running = true;
}
}
if (!Input.GetKey("x"))
{
if (Running == true)
{
PlayerSpeed = PlayerSpeed / 2;
Running = false;
}
}
}
void switchside()
{
canSideWalk = true;
}
void StartBattle()
{
Music1.SetActive(false);
Music2.SetActive(false);
Music3.SetActive(false);
Music4.SetActive(false);
Music5.SetActive(true);
Music6.SetActive(false);
battle.enabled = true;
canwalk = true;
playerControl.enabled = false;
openMenu.enabled = false;
}
what i think the problem is that there is no way to deter$$anonymous$$e whether the player is still running or not. I think you should use Raycasting here.
if (position of ray is constantly changing
&& input.getkeydown(keycode.x))
{
running == true;
//use a coroutine here as a cool down system
startcoroutine(resetrunning());
}
IEnumerator Resetrunning()
{
yield return new WaitForSeconds(0.1f);
running = false;
}
Running and canwalk are just for dialogue and a run button. When you move, the animation plays and the encounter chance is generated so the add force bit should work too. It won't move period.
try using if else ladder ins$$anonymous$$d of just if. $$anonymous$$ight work.
I still don't think that will work. Even if gravity is turned on for the Rigidbody, it won't move an inch
Answer by Razputin · Jun 27, 2019 at 06:59 AM
Did you set the rigidbody to kinematic?
it doesn't matter if it's on or off, it still won't move
Answer by danielharush5252 · Jun 27, 2019 at 09:10 AM
if (canwalk == true)
why are there 2 =
try changing it to
if (canwalk = true)
Actually, the right thing to see if a variable is true is the following:
if(canwalk){
//Some code
}
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Raycast not working correct 0 Answers
Positioning UI objects on 2D Game Objects 0 Answers
Scrollable rect containing GUIText/Label 1 Answer
Moving an object object x pos + speed?? 0 Answers