Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Szkut · May 21, 2020 at 06:40 PM · rotationcharacterlookattarget

Limit character head rotation

Hello. I have a character who look at the point where the cursor is. Now he rotate a head 360 degrees in all axes. And i wont to limit X,Y rotation. I dont know how to do that.

public class HeadLook : MonoBehaviour { public LayerMask layerIgnore; public Camera cam; public GameObject head; public Vector3 limit; private RaycastHit hit;

 private void LateUpdate()
 {
     followCursor();
 }

 private void followCursor()
 {
     Vector3 cursorPosition = Input.mousePosition;
     Ray ray = cam.ScreenPointToRay(cursorPosition);

     if (Physics.Raycast(ray, out hit, 500f, ~layerIgnore))
     {
         head.transform.LookAt(hit.point);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tadadosi · May 21, 2020 at 08:56 PM

This is a different approach that might work for you. Move an empty gameobject to the mouse position and add constraints to that movement, and then just add a transform.LookAt to your head to look at that point.


The script for the point is this:

 using UnityEngine;

 public class PointToLookAt : MonoBehaviour
 {
     public float posMultiplier = 3f;
     public float pointDepth = 0.2f;
     public bool lockX;
     public bool lockY;
     public Camera cam;
 
     void Update()
     {
         Vector3 mousePos = Input.mousePosition * posMultiplier;
         mousePos.z = pointDepth;
         Vector3 target = cam.ScreenToWorldPoint(mousePos);
 
         if (lockX)
         {
             target.x = 0;
         }
         if (lockY)
         {
             target.y = 0;
         }
         transform.position = target;
     }
 }


Simple LookAt for the head:

 using UnityEngine;
 
 public class LookAtTarget : MonoBehaviour
 {
     public Transform target;
 
     void Update()
     {
         transform.LookAt(target.position);
     }
 }



And in the Inspector it will look like this for the point: alt text


And like this for the simple LookAt on the head: alt text


unity-simple-lookat-script.png (7.9 kB)
unity-script-to-move-point-to-mouse-position-with.png (11.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Fkat · May 21, 2020 at 10:55 PM

Here is yet another method:

  float x;
  float y;
 
  private void Update()
  {
      x -= Input.GetAxisRaw("Mouse Y") * moveSensitivity * Time.deltaTime;
      y += Input.GetAxisRaw("Mouse X") * moveSensitivity * Time.deltaTime;
      x = Mathf.Clamp(x, -90, 90);
      transform.rotation = Quaternion.Euler(x, y, 0);
  }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Szkut · May 22, 2020 at 09:19 AM 0
Share

This method cant help me because i dont use $$anonymous$$ouseX and $$anonymous$$ouseY to rotation GameObject. but thanks for answer ;)

avatar image
0

Answer by javiolvecal · May 21, 2020 at 06:52 PM

https://www.youtube.com/watch?v=_QajrabyTJc&t=598s
In this tutorial you will find all you need :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Szkut · May 21, 2020 at 07:52 PM 0
Share

I dont know how to convert this to my look at hit.point position because he use mouseX and mouseY.

avatar image
0

Answer by Szkut · May 22, 2020 at 09:15 AM

Thanks @Fkat and @tadadosi I don't know if these solutions would help in my case because now I was able to solve the problem. This https://answers.unity.com/questions/1153173/turret-rotation-based-on-its-own-local-rotation.html help me so much.

my code now:

 using UnityEngine;
 public class HeadLook : MonoBehaviour
 {
 public LayerMask layerIgnore;
 public Camera cam;
 public GameObject head;
 public Vector3 limit;
 public float smooth;
 private Vector3 target;
 private RaycastHit hit;
 private Vector3 normal;
 private void LateUpdate()
 {
     followCursor();
 }
 private void followCursor()
 {
     Vector3 cursorPosition = Input.mousePosition;
     Ray ray = cam.ScreenPointToRay(cursorPosition);

     normal = transform.forward;

     if (Physics.Raycast(ray, out hit, 100f, ~layerIgnore))
     {
         Vector3 desiredPosition = new Vector3(hit.point.x, hit.point.y, hit.point.z);
         Vector3 smoothedPosition = Vector3.Lerp(target, desiredPosition, smooth);
         target = smoothedPosition;

         Vector3 direction = target - head.transform.position;
         Debug.DrawRay(head.transform.position, direction, Color.blue);

         float angle = Vector3.Angle(normal, direction.normalized);

         Vector3 axis = Vector3.Cross(normal, direction.normalized);          

         Vector3 targetDir = Quaternion.AngleAxis(Mathf.Clamp(angle, -limit.x, limit.x), axis) * normal;
         Debug.DrawRay(transform.position, targetDir, Color.red);

         Quaternion targetRotation = Quaternion.LookRotation(targetDir);
         head.transform.rotation = Quaternion.Slerp(head.transform.localRotation, targetRotation, 1);                       
     }       
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object Look At Mouse 2 Answers

2D LookAt not working as intended 1 Answer

Lock rotation axis? 4 Answers

Rotate my torso in vertical axis towards crosshair (target) 0 Answers

Bone lookat do not work Help! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges