Question by
budynkamil3 · Jun 27, 2020 at 06:41 PM ·
movementscript.jumpjumpingthird-person
Need help with jumping on third person movement script.
Hello! I'm new in Unity community, also new in scripting. I have code to control my 3d object with third person camera. But i can't find info how to make my object jump. One more thing, when i set my object on the ground and hit play, it's automaticly goes 1m up - levitates, idk why.. Please help :)
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 3f;
public float turnSmoothTime = 0.1f;
public float turnSmoothVelocity;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
Comment