How do you stop capsule from going through walls?
Hi, I am making a game but my player (capsule) keeps walking through all the houses. I have been trying to add wall colliders but they are not working. (its 3rd person) Here is my movement code.
using UnityEngine;
public class playerController : MonoBehaviour {
public float mSpeed;
// Use this for initialization;
void Start () {
mSpeed = 10f;
}
// Update is called once per frame
void Update () {
transform.Translate(mSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, mSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
}
} here is the 3rd person camera code; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPerspnScript: MonoBehaviour { public GameObject target; public float rotateSpeed = 5;
void Start()
{
transform.parent = target.transform;
transform.LookAt(target.transform);
}
void LateUpdate()
{
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
target.transform.RotateAround(target.transform.position, Vector3.up, horizontal);
target.transform.RotateAround(target.transform.position, Vector3.left, vertical);
}
} finally my mouse aim code; using UnityEngine; public class MouseAimCamera : MonoBehaviour { public GameObject target; public float rotateSpeed = 100f; Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
target.transform.Rotate(0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
Answer by iBicha · Jul 29, 2017 at 01:55 AM
Few things to check:
1-the colliders are not triggers (in the inspector, see if Is Trigger is unchecked)
2-it is best to add a rigid body to your controller, and translating it through adding force. It will let Physics take care of correctly colliding with other objects in the scene without weird artifacts.
Your answer
Follow this Question
Related Questions
OnCollisionEnter not running. 1 Answer
How do i calculate mesh on a spline?? 0 Answers
How do I optimize my code?,How do I optimize my script? 1 Answer
How to move whilst airborne (using standard third person character script)? 0 Answers
I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer