Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

640
Views
Unity 3d my character does not rotate to the raycasthit point I drew

I draw a rail on my central character with the direction of the front of my camera. I use Rigidbody.LookRotation to make the character return to that point, but it doesn't.

private void Update()
{
    Physics.Raycast(transform.position + new Vector3(0, 3, 0), mainCam.transform.forward, out hit);
    Quaternion lookrotation = Quaternion.LookRotation(new Vector3(hit.point.x,0,hit.point.z));
    transform.rotation = Quaternion.Slerp(transform.rotation, lookrotation, Time.deltaTime * 2);
    Debug.DrawLine(transform.position + new Vector3(0, 3, 0),hit.point);
    Debug.Log(hit.collider.gameObject.name);
}

My Scene

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

At first glance there are two points:

  1. LookRotation expects a forward direction .. currently you only give it a position.

    you would rather want to use e.g.

    if(Physics.Raycast(transform.position + new Vector3(0, 3, 0), mainCam.transform.forward, out var hit))
    {
        var direction = hit.point - transform.position;
        direction.y = 0;
        var lookrotation = Quaternion.LookRotation(direction);
        ...
    }
    
  2. currently you also rotate just real slow. Assuming 60 Fps Each frame you interpolate with a factor of about 1/60 * 2 so about 0.033.. from the current towards the target rotation. So this gets even slower the closer you get to the target rotation.

    You would rather either not multiply by Time.deltaTime but use a constant factor e.g. 0.5f

    or use RotateTowards and provide a continous anglePerSecond * Time.deltaTime.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!