Using a camera as a child on box collider with MouseLookPlus, which way is up with euler angles?
I am attempting to make a first person character controller that can look with the mouse and jump to walls or ceilings then change the orientation so the walls or ceiling become the floor, like a person with magnetic boots in zero gravity. I have used the answer in the following script to make this work but am having difficulty adapting it to work with MouseLookPlus. It all seems to work except the angle that I get for my Raycast appears to travel up instead of directly where the camera is looking. The following is the code that I am using to get the character to jump to the wall (or ceiling).
function Update() {
// Jumping, ToWall or ordinary jump
if (jumping) return; // Don't do update while moving to a wall
var rb = GetComponent.<Rigidbody>();
var camArray : Camera[] = GetComponentsInChildren.<Camera>();
var cam : Camera = camArray[0];
var ray : Ray;
var hit : RaycastHit;
var viewAngle : Vector3;
viewAngle = Vector3(cam.transform.localEulerAngles.x, rb.transform.localEulerAngles.y, rb.transform.localEulerAngles.z);
if (Input.GetButtonDown("Jump")) { // If 'jump' pressed do raycast
ray = Ray(transform.position, viewAngle);
if (Physics.Raycast(ray, hit, jumpRange)) {
JumpToWall(hit.point, hit.normal); // Then jump to wall
}
else if (isGrounded) {
rb.velocity += jumpSpeed * myNormal; // Or normal jump
}
}
And the following code is the processing of the jump itself:
function JumpToWall(point : Vector3, normal : Vector3) {
jumping = true; // Character is jumping
var rb = GetComponent.<Rigidbody>();
rb.isKinematic = true; // Turn physics off while jumping
myNormal = normal;
var camArray : Camera[] = GetComponentsInChildren.<Camera>();
var cam : Camera = camArray[0];
var viewAngle = Vector3(cam.transform.localEulerAngles.x, rb.transform.localEulerAngles.y, rb.transform.localEulerAngles.z);
var orgPos = transform.position;
var orgRot = Quaternion.Euler(viewAngle);
var dstPos = point + normal * (distGround + 0.5); //jumps to 0.5 above wall
var myForward = rb.transform.forward;
var dstRot = Quaternion.LookRotation(myForward, normal);
for (var t : float = 0.0; t < 1.0; ) {
t += Time.deltaTime;
transform.position = Vector3.Lerp(orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
yield;
}
myNormal = normal; // update normal
rb.isKinematic = false; //turn physics back on
jumping = false; // jump has concluded
}
Any assistance would be greatly appreciated, thank you in advance.
The code computing ViewAngle doesn't look even close to correct, for the input to a raycast. You should be able to search for the direction a camera is facing, and how to make a ray.
Thanks for pointing me in the right direction Owen, I found 'ViewportPointToRay' in Camera and it works a treat. Now to iron out a few more bugs and I should have it working.
Your answer
![](https://koobas.hobune.stream/wayback/20220612071021im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate object A based on camera y rotation if angle is grather or less then 0 Answers
Problem rotating camera on negative z axis 1 Answer
Using DampSmooth in EulerAngle is affecting the Position.Z value of the gameObject. 0 Answers
camera doesn't work after reloading the scene 0 Answers
Why doesn't my camera work?,Why does my mouse keep going up? 2 Answers