- Home /
Question by
ttroenigk · Aug 03, 2020 at 11:25 AM ·
c#cameracamera rotatecamera-lookinput.getaxis
Help My camera rotation script when a player hits a wall or game object it goes crazy. How can i change it to prevent this.
public class CameraController : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float turnSpeed = 4.0f;
//public float rotateSpeed;
public Transform pivot;
// Start is called before the first frame update
void Start()
{
offset = target.position - transform.position;
pivot.transform.position = target.transform.position;
pivot.transform.parent = target.transform;
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Mouse X") * turnSpeed;
float vertical = Input.GetAxis("Mouse Y") * turnSpeed;
if (pivot.rotation.eulerAngles.x > 45f && pivot.rotation.eulerAngles.x < 180f)
{
pivot.rotation = Quaternion.Euler(45f, 0, 0);
}
if (pivot.rotation.eulerAngles.x > 180 && pivot.rotation.eulerAngles.x < 315f)
{
pivot.rotation = Quaternion.Euler(315f, 0, 0);
}
float desiredYAngle = target.eulerAngles.y;
float desiredXAngle = pivot.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
transform.position = target.position - (rotation * offset);
//transform.position = target.position - offset;
transform.LookAt(target);
if (Input.GetMouseButton(1))
{
//offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
target.Rotate(0, horizontal, 0);
pivot.Rotate(-vertical, 0, 0);
if (transform.position.y < target.position.y)
{
transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
}
}
}
}
camera code above
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public Rigidbody rb;
public float jumpForce;
public int jumpNumber;
public int currentJumpNumber;
public Vector3 moveDirection;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
public bool isGrounded = false;
// Update is called once per frame
void Update()
{
//rb.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
//float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxis("Vertical"))+(transform.right*Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized*moveSpeed;
//moveDirection.y=yStore;
isGrounded = Physics.Raycast(transform.position, Vector3.down, distanceToGround);
if (rb.velocity.y <= 0 && isGrounded)
{
currentJumpNumber = 0;
}
if (Input.GetButtonDown("Jump")&¤tJumpNumber<jumpNumber)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
currentJumpNumber++;
}
rb.MovePosition(rb.position + moveDirection*Time.deltaTime);
}
public float distanceToGround = 0.5f;
//bool IsGrounded()
//{
//return Physics.Raycast(transform.position, Vector3.down, distanceToGround);
//}
}
player movement above
so when I hit a object the camera goes crazy. this works perfectly otherwise. if i can get my current camera update part edited and or the player movement move direction edited so it doesn't do this behavior i'd appreciate it. been trying for 2-3 days cant find a working way to do it
Comment
Your answer
Follow this Question
Related Questions
How to rotate player based off my current code 0 Answers
How to make the player direction change with the camera rotation? 1 Answer
How to make my Camera Controller look at target 0 Answers
Free camera look question 2 Answers
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer