using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float moveSpeed; private Rigidbody myRigidbody; private Vector3 moveInput; private Vector3 moveVelocity; private Camera mainCamera; // Start is called before the first frame update void Start() { myRigidbody = GetComponent(); mainCamera = FindObjectOfType(); } // Update is called once per frame void Update() { moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")); moveVelocity = moveInput * moveSpeed; Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayLenght; if (groundPlane.Raycast(cameraRay, out rayLenght)) { Vector3 pointToLook = cameraRay.GetPoint(rayLenght); Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue); transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z)); } } void FixedUpdate() { myRigidbody.velocity = moveVelocity; } }
That was helpful ❤
my man thank u so much
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
private Rigidbody myRigidbody;
private Vector3 moveInput;
private Vector3 moveVelocity;
private Camera mainCamera;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent();
mainCamera = FindObjectOfType();
}
// Update is called once per frame
void Update()
{
moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput * moveSpeed;
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLenght;
if (groundPlane.Raycast(cameraRay, out rayLenght))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLenght);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
void FixedUpdate()
{
myRigidbody.velocity = moveVelocity;
}
}
thank you alot