Hey So Im trying to use to raycast for the player to follow the mouse position, but the player is facing over to the left while the mouse curser is over on the right. I can’t find a solution! How can I offset this or tell it where the front of the player is?
You get a raycast position from the ground.
Then you have a look direction which is the raycast position - the position of the player.
Set the Y to 0 (Which your ground probably already is)
But then you do a transform.LookAt where you re-add the lookDir to the player position. Which you’ve just subtracted, so it would be the same value as lookPos again. Vector3 lookDir = lookPos - transform.position; transform.position + lookDir
just to virtualize values for example:
100 = 110 - 10
10 + 100 = 110
If you do lookDir.y = 0, you’ll look at the ground. But your ground point is probably already Y = 0.
Doing a LookAt at a Y axis other than its own, it’ll rotate forward or backward depending whether the point is lower or higher than the character itself.
You’re also polling user input in the FixedUpdate loop. That should be done in Update instead.
So poll the user input in Update, store the values and use them in FixedUpdate.
(though in my example below, I don’t use the mouse input.)
Instead of calculating the direction the player should move to, you could probably just use transform.forward.
Rotation is done by using the LookAt and the forward direction is where it should move to.
I changed the script up a bit as I was debugging the values.
You could try this on a capsule.
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
// Other Components
public CharacterController Controller;
private Camera mainCamera;
// Public Modifiers
public float MoveSpeed = 6f;
// Private variables
private Vector3 lookPos;
private void Start()
{
mainCamera = Camera.main;
}
// Update is called once per frame
private void Update()
{
// Raycast from the screen point to the world with a distance of max 100 meters.
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hit, 100))
{
// Look position is the point where the raycast hit something.
lookPos = hit.point;
// Keep the height of where to look at, at the same height of the character.
lookPos.y = transform.position.y;
}
transform.LookAt(lookPos, Vector3.up);
}
// FixedUpdate is called at a fixed interval, which can be multiple times a frame or even skipping one.
private void FixedUpdate()
{
// Likely I'm wrong to use Time.deltaTime here as well.
Controller.Move(transform.forward * MoveSpeed * Time.deltaTime);
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Debug.DrawLine(transform.position, lookPos, Color.red);
}
private void OnValidate()
{
if (Controller == null)
{
Controller = GetComponent<CharacterController>();
UnityEditor.EditorUtility.SetDirty(this);
}
}
#endif
}
Thanks for those, I will check them out. My coding experience is from years back and I will be going back to it at some point to relearn everything, but it in the mean time it will be messy
could animation rigging cause my problem? I’m probably going to rewrite the entire look code or use a new model and see if anything changes
So I fixed the issue be reseting everything. But it’s still central to the character opposed to the gun position. How can I change this?
I made a firepoint for the end of the gun but not sure how to implement this?
Or could i offset the players rotation in script to account for this?
I was wondering if to change this line of code? So it’s not from camera position. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);