Question by
coridoody · Nov 12, 2020 at 05:52 AM ·
jumpingfirst person controllerboost
I want to boost my FPS character in the direction my mouse/camera is pointing
I used the Brackeys FPS Controller Tutorial to make this script. and I want to make the character boost toward the mouse/camara's direction I really can't figure out how to to it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Camera cam;
public float speed = 12;
public float originalSpeed;
Vector3 velocity;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.3f;
public LayerMask groundMask;
bool isGrounded;
public float JumpHieghtInInGameUnits;
private void Start()
{
originalSpeed = speed;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
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 * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHieghtInInGameUnits * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
I'll also provide the camera look script here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Camera cam;
public float speed = 12;
public float originalSpeed;
Vector3 velocity;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.3f;
public LayerMask groundMask;
bool isGrounded;
public float JumpHieghtInInGameUnits;
private void Start()
{
originalSpeed = speed;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
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 * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHieghtInInGameUnits * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
any help would be greatly appreciated, Thanks! (:
Comment
Your answer