- Home /
Weird bug. Roll a ball project
I'v been working on the roll-a-ball project from the learn section. I recently added a "Speed" text field and I noticed that when I first start playing, if press W or S then the speed will not go above 3.8 (or below -3.8). If I go left or right just a hair, this will fix the problem and I can move with W and S as normal. The same fix happens if I bump the ball into a wall. The glitch also does not occur if I start the ball out at (.1, .5, 0) in the editor instead of (0, .5, 0). Just incase I somehow screwed the code up I'll paste it below.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float accelSpeed;
private int pickupCount;
public Text countText;
public Text winText;
public Text speedGage;
void Start(){
pickupCount = 0;
setCountText();
winText.text = "";
speedGage.text = "0";
}
void FixedUpdate(){
updateMovement();
}
void updateMovement(){
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0,moveVertical);
rigidbody.AddForce(movement * moveSpeed * Time.deltaTime);
speedGage.text = rigidbody.velocity.ToString();
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "PickUp"){
other.gameObject.SetActive(false);
pickupCount += 1;
setCountText();
}
}
void setCountText(){
countText.text = "Count: " + pickupCount.ToString();
if(pickupCount == 16){
winText.text = "You Win!";
}
}
}
Your problem is probably in the level. $$anonymous$$aybe a collier you deleted the renderer on which is blocking the ball. Try building a new scene and placing only the ball and a platform in it. The script looks right to me. I don't know why you put the movement information in it's own script ins$$anonymous$$d of directly in fixed update. That's just making things very slightly less efficient.
It's not that. There are only 5 or so items in the hierarchy, of which none are touching the player. I also tried moving the x to .000000001 ins$$anonymous$$d of 0. It still works perfectly. I doubt something like this would avoid a collider.
In that case I would guess it's just a bug. You're not doing anything wrong as far as I can tell. $$anonymous$$aybe someone else can figure it out. Sorry.