Question by
petec4uti0n · Aug 11, 2020 at 10:17 AM ·
camerascripting problemmovementfirst person controller
How to make FPS character jump in the direction my camera is facing?
So i've been following Brackeys tutorial on FPS Movement and i'm now trying to add my own mechanics, though now i'm stuck. Basically i want to do a jump (bounce) in the direction my player camera is facing, however the camera is a child of my player controller, and since it has it's own script for mouse movement etc, I cant get the direction from it and apply velocity in said direction from my player controller script. Anything i've tried so far hasn't worked, which is why i'm here now
My Player controller script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyBox;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public string characterClass;
public CharacterController controller;
// Movement
public float movementSpeed = 6f;
public float jumpHeight = 0.75f;
public bool canBounce;
[ConditionalField("canBounce")]
public float bounceHeight = 2f;
public float gravity = -9.82f;
public float groundDistance = 0.4f;
public Transform GroundCheck;
public LayerMask groundLayer;
bool isGrounded;
Vector3 velocity;
// Health
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
// Update is called once on Start
void Awake()
{
// Set Max HHealth
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
// Movement
isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundLayer);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * movementSpeed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (Input.GetMouseButtonDown(2))
{
Bounce();
}
}
void Jump()
{
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
Debug.Log("Jumped!");
}
}
void Bounce()
{
**I need help here** = Mathf.Sqrt(bounceHeight * -2f * gravity);
Debug.Log("Bounced!");
}
}
And my Camera controller script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerTransform;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerTransform.Rotate(Vector3.up * mouseX);
}
}
How would i make my character jump (bounce) in the direction of my camera? (it's the bounce() function im trying to create here)
Comment