I’ve started working on a new project and im having a little bit of trouble getting my character (simple cube object) from going straight through my walls (also cube objects). my player has a rigidbody and both the walls and player have box colliders.
public float speed;
private Rigidbody rb;
// Use this for initialization
void Start ()
{
rb = GetComponent ();
}
// Update is called once per frame
void Update ()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
well unity says to only put rigidbody on moving objects (like my player) when i add a rigidbody to the wall i go through it. when i remove it, the wall stops my character like it should, but if i follow the wall to a corner. ill just shoot out of the map
Please use code tags: Using code tags properly - Unity Engine - Unity Discussions
There are a few issues with this approach, but the main one is that you are force moving the rigidbody, using Rigidbody.MovePosition(), which does not cause collisions to happen. Your easiest solution would be to set your rigidbodys velocity:
rb.velocity = movement * speed;
This should yield an almost identical movement, but constrained by collisions.
Also, when dealing with physics you should be using FixedUpdate rather than Update, as the fixed one is tied to the physics loop of your game.
@ThermalFusion i moved it down into FixedUpdate and now its saying movement is undefined, so i move my new vector down into fixed aswell, but then the axis werent defined… so is it all supposed to be in fixed update?
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical")
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
}
EDIT: even with everything under fixedupdate movement is still undefined
public float speed;
public Text CoinCounter;
private int count;
private Rigidbody rb;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCoinCounter ();
}
// Update is called once per frame
void Update ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical")
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
}
void FixedUpdate ()
{
rb.velocity = movement * speed;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Coin"))
other.gameObject.SetActive (false);
count = count + 1;
SetCoinCounter ();
}
}
“Unexpected symbol ‘Vector3’”
“SetCoinCounter” and “Text” do not exist.
I added the SetCoinCounter and all of that to try to add the collectible coins in my game. i used a similar code to the one in the roll a ball game, but using different names for them obviously, and it works perfectly fine in that game, but not in mine. am i missing a part of it some where along the line? al of my coins are properly tagged