Please help, there is no error at the console tag but the character don´t rotate or react with the mouse position.
//Hi Guys, i`m just starting with Unity and recently buy a tutorial for a basic survival shooter videogame. but I can`t make that the character rotate with the mouse position or to move across the stage.
Please let me know if i have to set some kind of permission the tracking for de mouse at the Unity preferences or whats my error so i can continue learning.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
private Animator anima;
bool walking;
Vector3 movimiento;
int floorMask;
public float camRayLength = 100f;
public float speed = 2f;
Rigidbody rb;
Vector3 move;
void Start () {
rb = GetComponent<Rigidbody>();
anima = GetComponent<Animator>();
floorMask = LayerMask.GetMask("floor");
}
private void FixedUpdate()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Animation(h, v);
MyRotation();
Moving(h, v);
}
public void Animation(float h , float v )
{
walking = h != 0f || v != 0f;
anima.SetBool("isWalking", walking);
}
void Moving(float h, float v)
{
move.Set(h, 0f, v);
move = move.normalized * speed * Time.deltaTime;
if (rb)
rb.MovePosition(transform.position + move);
}
public void MyRotation()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit; //Variable para pasar la referencia de la posicion del mouse.
if ( Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
{
movimiento = floorHit.point - transform.position;
movimiento.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(movimiento);
rb.MoveRotation (newRotation);
}
}
// Update is called once per frame
void Update () {
}
}
$$anonymous$$ost likely:
You didn't attached a Rigidbody component to the gameObject
There is no gameObject in the
floor
layer in your scene, or at least, the camera is not looking at it.
Are you sure you don't have any error in the console? Are you sure the 3 buttons at the top right of the Console tab are checked?
Hi @Hellium many thanks for your answer, I check all the things that you mentioned non of them fix the problem. I continue having no errors at the console, and the GO doesn't rotate with the mouse position.
Its been 10 days since I am trying to figure out the solution, I´m even using free assets and I copy the code exactly as the tutorial said.
But every
Oh, I think I see the problem. Physics.Raycast needs a bit mask. However, you provided a simple layer index. Use this ins$$anonymous$$d
if ( Physics.Raycast(camRay, out floorHit, camRayLength, 1 << floor$$anonymous$$ask))
If it still does not work, use this to debug and understand the problem:
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit; //Variable para pasar la referencia de la posicion del mouse.
Debug.DrawRay( camRay.origin, camRay.direction * camRayLength, Color.red ) ;
if ( Physics.Raycast(camRay, out floorHit, camRayLength, 1 << floor$$anonymous$$ask))
{
Debug.Log("Object touched : " + floorHit.collider.name, floorHit.collider ) ;
movimiento = floorHit.point - transform.position;
movimiento.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(movimiento);
Debug.DrawRay( rb.position, movimiento * camRayLength * 0.1f, Color.cyan ) ;
rb.$$anonymous$$oveRotation (newRotation);
}
Your answer
Follow this Question
Related Questions
How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 0 Answers
Character rotation, only happens when mouse is rotated around world centre (0,0,0) 0 Answers
3D Top Down shooter mouse follow inaccurate at some angles 0 Answers
Converting a rotation(degree) to a vector3(usable in a RAY) 1 Answer