I’m following a tutorial with a vertical and horizontal movement but I just want to add a horizontal movement. Also, The tutorial add camera scripting but I will use cinemachine so I decide to alterate a bit the code and create a new one based on the original.
After some changes this error apeared.
Assets\Scripts\Test.cs(32,22): error CS0029: Cannot implicitly convert type ‘float’ to ‘UnityEngine.Vector3’
I searched for answers in google but, since I’m a newbie, I didn’t understand how to solve the error.
any help, please?
Original code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerOneMovement : MonoBehaviour
{
private float horizontalMove;
private float verticalMove;
private Vector3 playerInput;
public CharacterController Player;
public float PlayerSpeed;
public float gravity;
public float fallVelocity;
public float jumpForce;
public Camera mainCamera;
private Vector3 camForward;
private Vector3 camRight;
private Vector3 movePlayer;
// Use this for initialization
void Start()
{
Player = GetComponent<CharacterController>();
}
// Funcion movimiento jugador.
void Update()
{
horizontalMove = Input.GetAxis("Horizontal");
verticalMove = Input.GetAxis("Vertical");
playerInput = new Vector3(horizontalMove, 0, verticalMove);
playerInput = Vector3.ClampMagnitude(playerInput, 1);
camDirection();
movePlayer = playerInput.x * camRight + playerInput.z * camForward;
movePlayer = movePlayer * PlayerSpeed;
Player.transform.LookAt(Player.transform.position + movePlayer);
SetGravity();
PlayerSkills();
Player.Move(movePlayer * Time.deltaTime);
Debug.Log(Player.velocity.magnitude);
}
// Funcion para la camara.
public void camDirection()
{
camForward = mainCamera.transform.forward;
camRight = mainCamera.transform.right;
camForward.y = 0;
camRight.y = 0;
camForward = camForward.normalized;
camRight = camRight.normalized;
}
// Funcion para habilidades.
public void PlayerSkills()
{
if (Player.isGrounded && Input.GetButtonDown("Jump"))
{
fallVelocity = jumpForce;
movePlayer.y = fallVelocity;
}
}
// Funcion para la gravedad.
public void SetGravity()
{
if (Player.isGrounded)
{
fallVelocity = -gravity * Time.deltaTime;
movePlayer.y = fallVelocity;
}
else
{
fallVelocity -= gravity * Time.deltaTime;
movePlayer.y = fallVelocity;
}
}
}
My code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private float horizontalMove;
public float PlayerSpeed;
public float gravity;
public float fallVelocity;
public float jumpForce;
public CharacterController Player;
private Vector3 movePlayer;
private Vector3 playerInput;
// Start is called before the first frame update
void Start()
{
Player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxis("Horizontal");
playerInput = new Vector3(horizontalMove, 0);
playerInput = Vector3.ClampMagnitude(playerInput, 1);
movePlayer = playerInput.x;
movePlayer = movePlayer * PlayerSpeed;
Player.transform.LookAt(Player.transform.position + movePlayer);
SetGravity();
PlayerSkills();
Player.Move(movePlayer * Time.deltaTime);
Debug.Log(Player.velocity.magnitude);
}
// Habilidades
public void PlayerSkills()
{
if (Player.isGrounded && Input.GetButtonDown("Jump"))
{
fallVelocity = jumpForce;
movePlayer.y = fallVelocity;
}
}
// Gravity
public void SetGravity()
{
if (Player.isGrounded)
{
fallVelocity = -gravity * Time.deltaTime;
movePlayer.y = fallVelocity;
}
else
{
fallVelocity -= gravity * Time.deltaTime;
movePlayer.y = fallVelocity;
}
}
}