- Home /
Rotating camera while wallrunning
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class wallrun : MonoBehaviour {
private RigidbodyFirstPersonController cc;
private Rigidbody rb;
private int jumpCount = 0;
private bool isWallL = false;
private bool IsWallR = false;
private RaycastHit hitR;
private RaycastHit hitL;
public bool wallTouch;
// Use this for initialization
void Start () {
cc = GetComponent<RigidbodyFirstPersonController> ();
rb = GetComponent<Rigidbody> ();
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "jumpWall")
wallTouch = true;
}
void OnCollisionExit (Collision col){
if (col.gameObject.tag == "jumpWall")
wallTouch = false;
}
// Update is called once per frame
void Update () {
if (IsWallR == true)
// here i want some script that makes the camera rotate about 45 degrees in left/right directions
if (isWallL == true)
// here i want some script that makes the camera rotate about 45 degrees in left/right directions
if (IsWallR == false && isWallL == false)
//here i want a script that makes the camera go back to normal again.
if (cc.Grounded)
jumpCount = 0;
if (!cc.Grounded && jumpCount <= 1) {
if (Physics.Raycast (transform.position, transform.right, out hitR, 1)) {
if (hitR.transform.tag == "jumpWall" && wallTouch == true && cc.movementSettings.Running == true && speedmeter.speed >= 0.133f) {
IsWallR = true;
isWallL = false;
jumpCount += 1;
rb.useGravity = false;
StartCoroutine (afterRun ());
}
}
if (Physics.Raycast (transform.position, -transform.right, out hitL, 1)) {
if (hitL.transform.tag == "jumpWall" && wallTouch == true && cc.movementSettings.Running == true && speedmeter.speed >= 0.0133f) {
isWallL = true;
IsWallR = false;
jumpCount += 1;
rb.useGravity = false;
StartCoroutine (afterRun ());
}
}
}
}
IEnumerator afterRun() {
yield return new WaitForSeconds (1);
isWallL = false;
IsWallR = false;
rb.useGravity = true;
}
}
How do you do this? It doesnt work with transform.rotate at all. Help appreciated.
Why doesn't it work with transform.rotate
? Could you explain a bit?
It does not work with transform.rotate, because it seems like it rotates 25 degrees more for every update, i need to set the rotation ins$$anonymous$$d of rotating it.
$$anonymous$$aybe like this?
var tilt = -20f; // change depending on left or right wall
var forward = transform.forward;
var up = (Vector3.up + transform.left * tilt).normalized;
transform.rotation = Quaternion.LookRotation(forward, up);
Answer by Artik2442 · May 30, 2020 at 03:30 PM
Maybe you can use Animation to do that...
So, create an Animator for your Camera. Then, you can rotate your camera to -12,5 degrees or 12,5 degrees (you have to make 2 animations). Use your RayCast to know which animation to use. I've personally done this and it works perfectly.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Can't stop camera from rotating on Z Axis 1 Answer
how would i make this work? 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer