- Home /
Character moving to mouse in third person camera
Hello, I want to make when I press W or UpArrow my character move to mouse, how i can do it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
private Rigidbody rigidBody;
private Transform playerPosition;
public float playerSpeed = 10f;
public float playerJump = 10f;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
playerPosition = GetComponent<Transform>();
}
void Update()
{
if (Physics.Raycast(playerPosition.position, Vector3.down, 1f) && Input.GetButtonDown("Jump"))
{
rigidBody.AddForce(Vector3.up * playerJump, ForceMode.Impulse);
}
}
private void FixedUpdate()
{
Vector3 move = Vector3.zero;
move.y = Input.GetAxis("Vertical");
move.x = Input.GetAxis("Horizontal");
float hitdist = 0.0f;
rigidBody.AddForce(move.x,0,move.y * playerSpeed);
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerSpeed * Time.deltaTime);
}
}
}
Comment
not just post code and tell nothing please make you question more understandable Like what is the problem and for that problem what have you done what things you achieved some trails and errors this community is for help developers in problem, not just make whole scripts when they ask
Your answer
Follow this Question
Related Questions
Camera follow mouse around the map 2 Answers
Make player face same direction as camera. 4 Answers
Third person camera; player rotation. 1 Answer
dragging camera with mouse results in camera jumping., 0 Answers