I am trying to create a gravity gun like in Half Life 2. The gravityGun script parents a cube to the main camera. The main camera is parented to the player. The player is a character controller.
My problem is that the cube passes through objects if the player walks into the wall. This is my Player Controller script. Can someone help me with this problem by ether stopping the player from moving forward or rotating the cube if it is colliding with a wall or moving the cube around in your hands much like in Half life? I should mention that the cube is a Rigidbody.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class firstPersonController : MonoBehaviour {
public float movementSpeed = 10;
public float mouseSensitivity = 2.5f;
public float mouseVertRange = 60;
public float jumpVelocity = 5;
private float verticalRoatation;
private float verticalVelocity;
private float forwardSpeed;
private float leftRightSpeed;
private float rotateLeftRight;
private Vector3 movement;
private CharacterController characterController;
// Use this for initialization
void Start () {
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
// Horizontal Rotation
rotateLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotateLeftRight, 0);
// Vertical Rotation
verticalRoatation += Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRoatation = Mathf.Clamp(verticalRoatation, -mouseVertRange, mouseVertRange);
Camera.main.transform.localRotation = Quaternion.Euler(-verticalRoatation,0 ,0);
// Jumping
if (characterController.isGrounded){
verticalVelocity = 0;
if(Input.GetButtonDown("Jump")){
verticalVelocity = jumpVelocity;
}
}
// Movement
forwardSpeed = Input.GetAxis("Forward / Backward") * movementSpeed;
leftRightSpeed = Input.GetAxis("Left / Right") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
movement = new Vector3(leftRightSpeed, verticalVelocity, forwardSpeed);
movement = transform.rotation * movement;
characterController.Move(movement * Time.deltaTime);
}
}