I'm having issues with collision and Physics
K so here's the scope of the issue. I'm trying to add a collider to my camera so that when it collides with walls etc it will roll around them without stopping the character instead of just going through them. However when the camera collides with something, like say the floor it seems to influence the characters position. For example if I look up and the camera hits the floor it will lift the character off the ground and for some reason still consider itself 'grounded'. So far I think I've soused out that it's trying to keep the character within the confines of the offset that is meant to position the camera when the scene starts. Below are the scripts i'm currently using for the character controller (CC1) and the camera controller (CC2):
(CC1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
#region Variables
public float MoveSpeed;
public float MaxSpeed;
public float Jump;
public float xMove;
public float zMove;
public bool CanMove;
public bool isGrounded;
public bool CanSprint;
public bool CollectingIndex;
[HideInInspector]
public Transform position;
//Remeber to drag child camera into variable
public Transform Cam;
public Vector3 input;
public Vector3 inputJump;
[HideInInspector]
public Rigidbody Player;
public Animator CharAnim;
#endregion
#region Start Functions
void Start()
{
CanMove = true;
MoveSpeed = 30;
MaxSpeed = 5f;
Jump = 5f;
Player = gameObject.GetComponent<Rigidbody>();
CharAnim = gameObject.GetComponent<Animator>();
}
#endregion
#region IEnums
public IEnumerator CollectingMain()
{
yield return new WaitForSeconds(5);
CharAnim.SetBool("Collecting", false);
yield return new WaitForSeconds(0.2f);
StopCoroutine("CollectingMain");
}
#endregion
void FixedUpdate()
{
ControlPlayer();
}
void OnCollisionEnter(Collision Col)
{
if(Col.gameObject.tag == ("Ground") && isGrounded == false)
{
isGrounded = true;
}
}
#region Player Movement
void ControlPlayer()
{
if(CanMove == true)
{
xMove = Input.GetAxis("Horizontal");
zMove = Input.GetAxis("Vertical");
//If the Movement speed is lower than Max then the move speed can be applied
if (GetComponent<Rigidbody>().velocity.magnitude < MaxSpeed)
{
GetComponent<Rigidbody>().AddForce(input * MoveSpeed);
}
//Player facing Postion
Vector3 facingrotation = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
if (facingrotation != Vector3.zero)
{
transform.forward = facingrotation;
}
}
else
{
Debug.Log("Movement is disabled!");
}
}
public void Update()
{
Vector3 movementX = Cam.transform.right * xMove;
Vector3 movementZ = Cam.transform.forward * zMove;
input = (movementX + movementZ);
#region IF Statements
//If the player has unlocked the ability to sprint then pressing Left Shift will allow them to. If not then they cannot sprint
if (CanSprint == true)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
MaxSpeed = 10f;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
MaxSpeed = 5f;
}
}
//Jump Command
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && CanMove == true)
{
Player.AddForce(new Vector3(0, Jump, 0), ForceMode.Impulse);
isGrounded = false;
}
if(CollectingIndex == true)
{
GetComponent<Rigidbody>().velocity = Vector3.zero;
GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
GetComponent<Rigidbody>().Sleep();
}
//Checks if character is collecting main collectables
if (CharAnim.GetBool("Collecting") == true)
{
StartCoroutine("CollectingMain");
}
#endregion
}
#endregion
}
(CC2)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
//lookAt is target
public Transform target;
public Transform camTransform;
public CharacterController CController;
private Camera cam;
public Vector3 offset;
private const float Y_Angle_Min = -50.0f;
private const float Y_Angle_Max = 50.0f;
public float smoothSpeed = 0.125f;
private float distance = 3f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 10.0f;
private float sensitivityY = 5.0f;
private void Start()
{
CController = gameObject.GetComponentInParent<CharacterController>();
camTransform = transform;
cam = Camera.main;
}
private void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Mathf.Clamp(currentY, Y_Angle_Min, Y_Angle_Max);
}
void LateUpdate()
{
if(CController.CanMove == true)
{
Vector3 dir = new Vector3(0, 0, -distance);
Vector3 dir = new Vector3(0, 0, -distance) + offset;
Quaternion rotation = Quaternion.Euler(-currentY, currentX, 0);
camTransform.position = target.position + rotation * dir;
camTransform.LookAt(target.position);
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}
Answer by wewewu · May 16, 2020 at 12:28 PM
Use Rigidbody.MovePosition or Rigidbody.MoveRotation instead of setting the position of the camera, that might work.