- Home /
Question by
Squammy_U · Nov 16, 2017 at 10:41 PM ·
3dplayermousepositionx-axis
How to align x-axis movements with mouse x movement.
So I have a script that moves a player across the mouse x-axis. If I were to move the mouse to quickly in one direction and back in the other the object becomes off-centered with the mouse on the screen. I want the object to be aligned perfectly with the pointer on the screen as I move the mouse left and right, but on the x-axis only. How would I go about changing the players x position with the mouse movement on the screen? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed;
public float gravity;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(Vector3.down * gravity);
float moveHorizontal = Input.GetAxis("Mouse X");
Vector3 movement = new Vector3(moveHorizontal, 0f, 0f);
rb.AddForce(movement * speed);
}
}
Comment
Your answer
Follow this Question
Related Questions
Getting the z of mouse position? 1 Answer
Virtual joystick problem issue 1 Answer
,Look rotation viewing vector is zero and character always facing 1 direction when idle 0 Answers
active ragdoll rotation is not working 0 Answers
3D mouse position 0 Answers