My camera keeps falling on y when I jump + move at the same time and eventually ends up going through the ground despite collision detection. I’m not sure why this is…has anyone ran across this before? If so, what can I do about it?
Here’s the code here…not pretty. D:
Use the mouse to move. Click a location and it will move there. spacebar to jump
myView.cs
using UnityEngine;
using System.Collections;
public class myView : MonoBehaviour {
public Transform myCam, playerObj;
public float mouseX, mouseY, prevMouseX, prevMouseY, xDiff, yDiff, mouseScroll, dist, distToGround;
public bool isTooClose, isTooFar, isGroundCollision, isBackCollision, hasMoved;
public RaycastHit hit;
void Start () {
prevMouseX = Input.mousePosition.x * Input.GetAxis("Mouse X");
prevMouseY = Input.mousePosition.y * Input.GetAxis("Mouse Y");
mouseX = 0;
mouseY = 0;
isTooClose = false;
isTooFar = false;
isBackCollision = false;
}
void Update () {
mouseScroll = Input.GetAxis("Mouse ScrollWheel");
mouseX = Input.mousePosition.x * Input.GetAxis("Mouse X");
mouseY = Input.mousePosition.y * Input.GetAxis("Mouse Y");
xDiff = mouseX - prevMouseX;
yDiff = mouseY - prevMouseY;
//rotate around player
#region rotate
if (Input.GetMouseButton(2) xDiff != 0)
{
myCam.RotateAround(playerObj.position, Vector3.up, 250* Input.GetAxis("Mouse X") * Time.deltaTime);
}
#endregion
//pan up/down
#region pan_On_Y
if (Input.GetMouseButton(2) yDiff != 0 isGroundCollision == false)
{
if (Physics.Raycast(myCam.position, -Vector3.up, out hit, 2))
{
isGroundCollision = true;
return;
}
myCam.Translate(Vector3.up * Input.GetAxis("Mouse Y"));
}
if (Input.GetMouseButton(2) yDiff != 0 isGroundCollision == true)
{
if (Input.GetAxis("Mouse Y") > 0)
{
myCam.Translate(Vector3.up * Input.GetAxis("Mouse Y"));
isGroundCollision = false;
}
}
#endregion
//Mouse zoom begin
#region mouse_zoom
if (mouseScroll != 0 isTooClose == false isTooFar == false isBackCollision == false)
{
if (dist < 4.0f)
{
isTooClose = true;
return;
}
if (dist > 22.0f)
{
isTooFar = true;
return;
}
if (Physics.Raycast(myCam.position, -myCam.forward, out hit, 3))
{
isBackCollision = true;
return;
}
myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
}
if (mouseScroll != 0 isTooClose == true)
{
float temp = Input.GetAxis("Mouse ScrollWheel");
if (temp < 0)
{
myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
isTooClose = false;
}
}
if (mouseScroll != 0 isTooFar == true)
{
float temp = Input.GetAxis("Mouse ScrollWheel");
if (temp > 0)
{
myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
isTooFar = false;
}
}
if (mouseScroll != 0 isBackCollision == true)
{
float temp = Input.GetAxis("Mouse ScrollWheel");
if (temp > 0)
{
myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
isBackCollision = false;
}
}
#endregion
Physics.Raycast(myCam.position, -Vector3.up, out hit, 25);
dist = Vector3.Distance(myCam.position, playerObj.position);
distToGround = Vector3.Distance(myCam.position, hit.point);
myCam.LookAt(playerObj.position);
prevMouseX = mouseX;
prevMouseY = mouseY;
}
public void updatePos(Vector3 amount)
{
myCam.position += amount;
}
/*This is jerky as hell and still goes through...but ignore this
*
* public void lockCamY(float y)
{
if (distToGround > 3.5f)
myCam.position = new Vector3(myCam.position.x, y, myCam.position.z);
else if (distToGround > 15)
myCam.position = new Vector3(myCam.position.x, distToGround - distToGround + 5, myCam.position.z);
else
myCam.position = new Vector3(myCam.position.x, y + 2, myCam.position.z);
}*/
player.cs
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public float moveSpeed, jumpSpeed, jumpCount, currJump, allowedJump, camYCache, airDrag, destX, destY, destZ;
public bool isJumping, isGrounded, isFalling, hasDblJumped, isMoving;
public Vector3 jumpVector, moveTo, initial;
public CharacterController controller;
public Transform user, cam;
public Ray ray;
public RaycastHit rcHit, hit;
public Quaternion rot;
public myView viewScript;
// Use this for initialization
void Start () {
moveSpeed = 900.0f;
jumpSpeed = 30.0f;
jumpCount = 0;
isJumping = false;
isFalling = false;
hasDblJumped = false;
controller = user.GetComponent<CharacterController>();
isGrounded = controller.isGrounded;
currJump = 0.0f;
allowedJump = 15.0f;
airDrag = 0.09f;
}
// Update is called once per frame
void Update () {
initial = user.position;
isGrounded = controller.isGrounded;
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out rcHit);
moveTo = rcHit.point;
moveTo.y = user.position.y;
rot = Quaternion.LookRotation(moveTo - user.position);
user.rotation = rot;
isMoving = true;
moveToPosition(rcHit.point);
}
if (isMoving == true)
{
moveToPosition(rcHit.point);
viewScript.updatePos(user.position - initial);
}
if (Input.GetKeyDown(KeyCode.Space) isJumping == false isFalling == false)
{
isJumping = true;
Jump();
}
if (isJumping == true isFalling == false)
{
Jump();
}
if (isFalling == true isJumping == false)
{
Fall();
}
}
public void Jump()
{
if (currJump < allowedJump)
{
float temp = 0.0f;
temp = Mathf.Sin(Time.deltaTime) * jumpSpeed;
currJump += temp;
controller.Move(Vector3.up * temp * Time.deltaTime * jumpSpeed);
}
else
{
isJumping = false;
isFalling = true;
}
}
public void Fall()
{
if (isGrounded == false)
{
float temp = 0.0f;
temp = Mathf.Sin(Time.deltaTime) * jumpSpeed;
currJump -= temp;
controller.Move(Vector3.up * temp * Time.deltaTime * jumpSpeed * -1);
}
else
{
isFalling = false;
currJump = 0;
}
}
public void moveToPosition(Vector3 targetPos)
{
if ((int)user.position.z != (int)targetPos.z)
controller.SimpleMove(user.forward * moveSpeed * Time.deltaTime);
else
isMoving = false;
}
}