- Home /
How to make an object rotate to raycast hit from mouse? Unity 3D
Sooooo. I was able to make my object move towards my raycast hit position from the mouse. But im having trouble making it rotate. It does rotate with a script i assembled together but, it does not rotate towards the raycast position. Im making a non-rts movement. So when i click it will move for a bit. But if i hold it will keep moving towards the direction of the raycast hit position. So its not rts or nav mesh movement.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class PlayerMovement : MonoBehaviour {
  public float speed = 10f;
  private Vector3 targetPos;
  private Vector3 startPos;
  private Vector3 mousePos;
 
 
  public void Start()
  {
      startPos = transform.position;
  }
 
  public void Update()
  {
      if (Input.GetMouseButton(0))
      {
          mousePos = Input.mousePosition;
          Ray mouseCast = Camera.main.ScreenPointToRay(mousePos);
          Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
          RaycastHit hit;
          float rayLength;
          if (Physics.Raycast(mouseCast, out hit, 100))
          {
              Debug.DrawLine(mousePos, targetPos, Color.blue);
              targetPos = new Vector3(hit.point.x, 2f, hit.point.z);
          }    
          
 
          if (groundPlane.Raycast(mouseCast, out rayLength))
          {
              Vector3 pointToLook = mouseCast.GetPoint(rayLength);
              transform.LookAt(new Vector3(pointToLook.z, transform.position.y, pointToLook.z));
          }
 
          transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
          
      }
 
      
  }
 
}
This code here is the movement plus the non-working rotation stuff in there. ANd its not working it will rotate but not in the right direction. Me is New.
Answer by Vega4Life · Dec 25, 2018 at 09:22 PM
I updated some code, but what I have works fine with my little test. Moves towards and looks at where I am click/click-holding. I also added a little movement margin, so the character wouldn't move/look twitch with little mouse movement. The main fix though... was just look at your targetPos.
 public void Update()
         {
             if (Input.GetMouseButton(0))
             {
                 mousePos = Input.mousePosition;
                 Ray mouseCast = Camera.main.ScreenPointToRay(mousePos);
                 Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
                 RaycastHit hit;
                 float rayLength;
                 if (Physics.Raycast(mouseCast, out hit, 100))
                 {
                     Debug.DrawLine(mousePos, targetPos, Color.blue);
                     targetPos = new Vector3(hit.point.x, 0f, hit.point.z);
 
                     // We need some distance margin with our movement
                     // or else the character could twitch back and forth with slight movement
                     if (Vector3.Distance(targetPos, transform.position) >= 0.5f)
                     {
                         transform.LookAt(targetPos);
                         transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
                     }
                 }
             }
 
         }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                