Colliders "offset" and going down too.
Well, Im just trying to create my own FPS Controller for my game to learn and well, you know, knowing what Im using. The thing is that I cant avoid the collider to go into the walls or, for example, if theres an spherical collider in the air, burying itself in the ground. Here's my script and my config. Jugador has rigidbody and box collider, Cube just has collider but even deactivating it still happens the same.
EDIT: You can see in the script that I've tried all the "movement" methods I have found.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraAdmin : MonoBehaviour
{
public GameObject Target;
Camera cameraPlayerCam;
private float sensibility = 1.5f;
float v;
float h;
private float velocidad = 0.01f;
private float run;
public float fuerzaSalto = 150f;
public bool isGrounded;
Ray rayAim;
private Rigidbody selfRigidbody;
private RaycastHit hit;
// Start is called before the first frame update
void Start()
{
if (GameObject.FindGameObjectWithTag("Player"))
{
Target = GameObject.FindGameObjectWithTag("Player");
}
selfRigidbody = Target.GetComponent<Rigidbody>();
cameraPlayerCam = GetComponent<Camera>();
cameraPlayerCam.transform.parent = Target.transform;
}
// Update is called once per frame
void Update()
{
h = sensibility * Input.GetAxis("Mouse X");
v = sensibility * Input.GetAxis("Mouse Y");
Target.transform.Rotate(0, h, 0);
cameraPlayerCam.transform.Rotate(-v, 0, 0);
rayAim = cameraPlayerCam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
cameraPlayerCam.transform.localPosition = new Vector3(0, 0.08f, 0.03f);
/*
if (Input.GetKey(KeyCode.W))
{
Target.transform.Translate(0, 0, velocidad * run);
}
if (Input.GetKey(KeyCode.D))
{
Target.transform.Translate(velocidad * run, 0, 0);
}
if (Input.GetKey(KeyCode.A))
{
Target.transform.Translate(-velocidad * run, 0, 0);
}
if (Input.GetKey(KeyCode.S))
{
Target.transform.Translate(0, 0, -velocidad * run);
}
*/
if (Input.GetKey(KeyCode.LeftShift))
{
run = 2f;
}
else
{
run = 1f;
}
if (Input.GetKey(KeyCode.V))
{
if (GameObject.FindGameObjectWithTag("Player") && !Target.CompareTag("Player"))
{
Target = GameObject.FindGameObjectWithTag("Player");
cameraPlayerCam.transform.parent = Target.transform;
cameraPlayerCam.transform.rotation = Target.transform.rotation;
selfRigidbody = Target.GetComponent<Rigidbody>();
}
}
if ((Input.GetKeyDown(KeyCode.Space)) && (isGrounded))
{
selfRigidbody.AddForce(0, fuerzaSalto, 0);
}
if (Input.GetKeyDown(KeyCode.F))
{
if (Physics.Raycast(rayAim, out hit, 15f))
{
if (hit.collider.CompareTag("NPC"))
{
Target = GameObject.Find(hit.collider.name);
cameraPlayerCam.transform.parent = Target.transform;
cameraPlayerCam.transform.rotation = Target.transform.rotation;
selfRigidbody = Target.GetComponent<Rigidbody>();
}
}
}
}
private void FixedUpdate()
{
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
selfRigidbody.position += z * transform.forward * Time.deltaTime * 1f;
selfRigidbody.position += x * transform.right * Time.deltaTime * 1f;
/*
if (Input.GetKey(KeyCode.W))
{
selfRigidbody.MovePosition(transform.position + (transform.forward * Time.deltaTime * velocidad));
}
if (Input.GetKey(KeyCode.D))
{
selfRigidbody.MovePosition(new Vector3(velocidad * run, 0, 0));
}
if (Input.GetKey(KeyCode.A))
{
selfRigidbody.MovePosition(new Vector3(-velocidad * run, 0, 0));
}
if (Input.GetKey(KeyCode.S))
{
selfRigidbody.MovePosition(new Vector3(0, 0, -velocidad * run));
}*/
}
}
Your answer
Follow this Question
Related Questions
Rigidbody going through Colliders help! 1 Answer
Need help with high speed collisions, sanity dwindling 1 Answer
How to make rigidbody not effect movement while still using it for collisions? 0 Answers
Player moving though walls while pressing two keys 0 Answers
Sphere movement with attached cube 0 Answers