Player is Going Through Walls
So I made a little 2d scene with the player and 4 walls but when ever I go to the wall i just go through it. I have Box Colliders, Mesh Colliders, and RigidBodys on all of my walls and Sphere Colliders, Mesh Colliders, and RigidBodys on my player as well. My Current Script is below . Im pretty sure that im not supposed to use "transform.position" because it doesnt look for collisons butIi dont know what to use then, and I'm not sure what setting the Box/Sphere Colliders, Mesh Colliders, and Rigid Bodys need to have.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermove : $$anonymous$$onoBehaviour {
float speed = 5.0f;
void Update () {
var moveh = new Vector3(Input.GetAxis("Horizontal"), 0);
var movev = new Vector3(0 ,(Input.GetAxis("Vertical")));
transform.position += moveh * speed * Time.deltaTime;
transform.position += movev * speed * Time.deltaTime;
}
}
Answer by Commoble · Mar 05, 2017 at 11:15 PM
Set the player object's Rigidbody type to dynamic, make sure the mesh collider on the player is set to convex, and use Rigidbody.MovePosition(Vector3) to set your new position each Update instead of setting the transform.position manually.
Rigidbodies on moving objects have to be dynamic if you want other colliders to prevent the rigidbody from moving through them. This is because the blocking is done by waiting for two colliders to overlap, then pushing the colliders away from each other. Kinematic and static rigidbodies can't be pushed (i.e. be blocked by objects), they can only push other other objects away (they can only block other objects).
Thank you very much I was stuck on this all last night.
Wait, now im getting some errors. It's telling me that ("Unity.Engine.Vector3" is a Type but a Varible was expected) and then it says (Expression denotes a type where a varible value or method group was expected.)
Okay so I tried changing some things and i ended up with this But it is telling me Vector3 is already defined in this scope. Could you please show me what my script is supposed to look like?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermove : $$anonymous$$onoBehaviour {
float speed = 5.0f;
privite Vector3;
void Update () {
var moveh = new Vector3(Input.GetAxis("Horizontal"), 0);
var movev = new Vector3(0 ,(Input.GetAxis("Vertical")));
Rigidbody.$$anonymous$$ovePosition Vector3 = moveh * speed * Time.deltaTime;
Rigidbody.$$anonymous$$ovePosition Vector3 = movev * speed * Time.deltaTime;
}
}
public Rigidbody body; // link your rigidbody to the script in the inspector
float speed = 0.5F;
void Update()
{
Vector3 oldPos = this.transform.position;
float newX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float newY = Input.GetAxis("Vertical") * speed * Time.deltaTime;
RigidBody.$$anonymous$$ovePosition(new Vector3(oldPos.x + newX, oldPos.y + newY, oldPos.z);
}
I suggest reading the documentation on a function whenever you use one that you've never used before.
Thank you very much but how do you link your rigidbody to the script in the inspector?
How do you link your rigidbody to the script in the Inspector?
Drag the rigidbody component to the script.
I strongly suggest reading through unity's documentation on scripting, before you jump into it, especially controlling gameobjects and components with scripts.
Answer by Willliam · Mar 06, 2017 at 01:15 AM
Try to add the Component ChracterController. and use ChracterController.Move(Vector3).
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object 0 Answers
How to Change Rigidbody Type OnCollision/Trigger with Script? 1 Answer
How to make other player phase through others 1 Answer
I added Rigidbody to my cube and now it goes super fast when i move! 0 Answers
Question about NavMesh and Rigidbody 0 Answers