well, 1 im new to game dev and C# in itself, ive been using some tutorials to understand that language a bit, got a working camera script and movement script, but im trying to add jump to it. I do have a vr game ive published so i know some of what im doing but still learning. This is meant to be a turn based open world rpg (similar to a dragon quest playstyle). 2. im not to sure where to go now. ive tried many things just dont know what im supposed to do.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Movement Settings")]
[SerializeField] float moveSpeed = 5f;
[SerializeField] float rotationSpeed = 500f;
/* set radius for ground check */
[Header("Ground Check Settings")]
[SerializeField] float groundCheckRadius = 0.2f;
[SerializeField] Vector3 groundCheckOffset;
[SerializeField] LayerMask groundLayer;
bool isGrounded;
float ySpeed;
Quaternion targetRotation;
CameraController cameraController;
Animator animator;
CharacterController characterController;
private void Awake()
{
cameraController = Camera.main.GetComponent<CameraController>();
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
}
private void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float moveAmount = Mathf.Clamp01(Mathf.Abs(h) + Mathf.Abs(v));
var moveInput = (new Vector3(h, 0, v)).normalized;
var moveDir = cameraController.PlanarRotation * moveInput;
var velocity = moveDir * moveSpeed;
velocity.y = ySpeed;
GroundCheck();
if (isGrounded)
{
ySpeed = -0.5f;
if(Input.GetButtonDown("Jump"))
{
velocity = 10f;
}
}
else
{
ySpeed += Physics.gravity.y * Time.deltaTime;
}
characterController.Move(velocity * Time.deltaTime);
if (moveAmount > 0)
{
targetRotation = Quaternion.LookRotation(moveDir);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
animator.SetFloat("moveAmount", moveAmount, 0.5f, Time.deltaTime);
}
void GroundCheck()
{
isGrounded = Physics.CheckSphere(transform.TransformPoint(groundCheckOffset), groundCheckRadius, groundLayer);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(0, 0, 1, 0.5f);
Gizmos.DrawSphere(transform.TransformPoint(groundCheckOffset), groundCheckRadius);
}
}
Thats what im currently at. I get a CS0029 saying
it cant be implicity convert type ‘float’ to ‘UnityEngine.Vector3’
without the jump stuff it works just fine. I want to add jumping to the game and maybe sprinting. so does anyone know how i would go about this? And any advice to learn the language better?
Edit: Sorry about forgeting the line, line 52. The jump velocity.