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 /
  • Help Room /
avatar image
1
Question by oneMinionLeft · Feb 14, 2016 at 03:07 AM · transformquaternionmousepositionslerpsmoothly

Rotate a GameObject smoothly towards mouse

Hi.

I'm very new to Unity, but I do have some programming experience. Mostly things like Powershell and Python to automate my job, so I'm not very familiar with graphical programming and physics.

I'm having some trouble rotating a GameObject towards my mouse. I know what I have to use Quaternion.Slerp to move it smoothly, but I can't for the life of me figure this out! I have managed to sort of make it rotate, but it's all wrong. I expect it's something to do with my rotateTo Quaternion, but I'm not sure.

Any help would be greatly appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
 
     private Vector3 mousePosition;
     public float moveSpeed = 2f;
     public float lookSpeed = 5f;
 
     void Start () {
 
     }
 
 
     void Update () {
         
         if (Input.GetMouseButton(1)) {
 
             MoveCharacter ();
             RotateCharacter ();
 
         }
 
     }
 
     void MoveCharacter() {
         
         mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.position = Vector2.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
 
         Debug.DrawLine (transform.position, mousePosition, Color.red);
 
     }
 
     void RotateCharacter() {
 
         float angle = Mathf.Atan2(Input.mousePosition.x, Input.mousePosition.y) * Mathf.Rad2Deg;
 
         Quaternion rotateTo = Quaternion.AngleAxis(angle, Vector3.forward);
     
         transform.rotation = Quaternion.Slerp(transform.rotation, rotateTo, Time.deltaTime * lookSpeed); 
 
     }
 
 }
 

If you run the script, you'll see a debug line in the scene editor. The line points to where I want to rotate my GameObject.

Comment
Add comment · Show 1
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 diogoico · Jan 09, 2018 at 02:07 PM 0
Share

Same problem here...,got the same problem...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fjonan · Jan 09, 2018 at 04:14 PM

Maybe you can use this script by @SebastianLague you can also find on github. He explains it very nicely in this tutorial here.

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
 
     public float moveSpeed = 6;
 
     Rigidbody rigidbody;
     Camera viewCamera;
     Vector3 velocity;
 
     void Start () {
         rigidbody = GetComponent<Rigidbody> ();
         viewCamera = Camera.main;
     }
 
     void Update () {
         Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
         transform.LookAt (mousePos + Vector3.up * transform.position.y);
         velocity = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical")).normalized * moveSpeed;
     }
 
     void FixedUpdate() {
         rigidbody.MovePosition (rigidbody.position + velocity * Time.fixedDeltaTime);
     }
 }
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

44 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

Related Questions

Can't really grasp how to keep my player level on X and Z while using LookRotation 0 Answers

[Solved] Smooth rotation with keydown 2 Answers

How to use Quaternion.Slerp with transform.LookAt? 3 Answers

unwanted rotation in y axis 0 Answers

Rotation problem 0 Answers


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