I’m not good at scripting, but I’ve managed to cobble together a script that works for 3d movement where the character rotates to face the direction of travel. However, I can’t figure out how to make the character jump. Can anyone help?
Here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement2 : MonoBehaviour
{
[SerializeField] private Rigidbody _rb;
[SerializeField] private float _speed = 5;
[SerializeField] private float _turnSpeed = 360;
private Vector3 _input;
public Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
GatherInput();
Look();
}
void FixedUpdate()
{
Move();
}
void GatherInput()
{
var velocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
_input = velocity;
animator.SetFloat("Speed", velocity.magnitude);
}
void Look()
{
if (_input != Vector3.zero)
{
var relative = (transform.position + _input) - transform.position;
var rot = Quaternion.LookRotation(relative, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, _turnSpeed * Time.deltaTime);
}
}
void Move()
{
_rb.MovePosition(transform.position + (transform.forward*_input.magnitude)* _speed * Time.deltaTime);
}
}