Hi,
this is my very first game project ever so I have no previous experience besides java programming.
For a learning project, I decided to create a top-down 3D voxel spaceship meteor shooter (keyboard, gamepad, eventually local multiplayer) to learn most basics fast.
Now, I got my character down and found some very helpful YT videos for the movement and when it came to aiming the spaceship I first decided to simply face the player to the mouse location on the screen by ray casting (movement and rotation independent from another). This works perfectly fine, but I want to implement a different system and don’t know how (and can’t find articles or videos that help).
Instead of always facing the player towards the mouse, I want it to rotate only when moving the mouse accordingly and save its rotation when the mouse is still. Currently, I can leave my mouse and orbit around it with my spaceship (like the rotation is locked on if you get what I mean).
Comparable to how top-down rotations with gamepads work; the player rotates as long as you rotate the joystick but it keeps its rotation if you let go of the stick.
How can I change my current script so it works clean?
Or do you have a YT tutorial I missed that explains all that?
EDIT:
This is what I try to replicate.
And this is what my movement currently looks like.
EDIT2:
I found out that Getting Over It has the exact controls I was looking for (despite the different camera angle), so I asked Bennett Foddy how he did it. This was his reply: translating the cursor’s local position to world space to make it interact with game objects in the scene.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovementBehaviour : MonoBehaviour
{
private Rigidbody sphereRigbod;
private PlayerInput playerInput;
private PlayerInputActions playerInputActions;
private Camera mainCamera;
public WeaponBehaviour weapon;
[SerializeField] private float speed = 70f;
private void Awake()
{
sphereRigbod = GetComponent<Rigidbody>();
playerInput = GetComponent<PlayerInput>();
playerInputActions = new PlayerInputActions();
playerInputActions.PlayerMovement.Enable(); //Enables EVERY Action map set up!
mainCamera = FindObjectOfType<Camera>();
}
private void FixedUpdate()
{
faceMouse();
//X Z Movement WASD
Vector2 inputVector = playerInputActions.PlayerMovement.Movement.ReadValue<Vector2>();
sphereRigbod.AddForce(new Vector3(inputVector.x, 0, inputVector.y) * speed, ForceMode.Force);
}
/* Faces the player character to the mouse on screen
*/
private void faceMouse()
{
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if(groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.red);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
}
EDIT: Replace the green “applied force” with “movement direction”