Hello all!
I’m experiencing a problem where I believe I’ve added all the code correctly, and am getting the correct screen to world conversion and registering the clicks (as evidenced by the debug log output), but when I play the simulation and click on the screen, the ship doesn’t move.
Here’s the setup:
Camera:
Player:
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float forceMagnitude;
[SerializeField] private float maxVelocity;
private Rigidbody rb;
private Camera mainCamera;
private Vector3 movementDirection;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
mainCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Touchscreen.current.primaryTouch.press.isPressed)
{
Vector2 TouchPosition = Touchscreen.current.primaryTouch.position.ReadValue();
Debug.Log(TouchPosition);
Vector3 worldPosition = mainCamera.ScreenToWorldPoint(TouchPosition);
Debug.Log(worldPosition);
movementDirection = transform.position - worldPosition;
movementDirection.z = 0f;
movementDirection.Normalize();
}
else
{
movementDirection = Vector3.zero;
}
void FixedUpdate()
{
if (movementDirection == Vector3.zero) { return; }
rb.AddForce(movementDirection * forceMagnitude * Time.deltaTime, ForceMode.Force);
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
}
}
}
everything seems to be in place for this to work, but something isn’t linking up correctly.
Thanks in advance for taking the time to look at this!