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 mnmwert · Jun 27, 2016 at 06:17 PM · rotationscripting problemmouse positiony-axislookatmouse

How do I make my player face the mouse on the Y axis in Unity C# 2D?

I have a c# script to make my player move towards the mouse but I need it to rotate to face the mouse. I have never dealt with these types of controls before and I've been trying for a long time to figure it out.

Here is my script:

using UnityEngine; using System.Collections;

public class playerController : MonoBehaviour { private Vector3 _target; public Camera Camera; public bool FollowMouse; public bool ShipAccelerates; public float ShipSpeed = 2.0f;

  void Start()
 {
     
 }

 public void OnEnable()
 {
     if (Camera == null)
     {
         throw new System.InvalidOperationException("Camera not set");
     }
     

 }

 public void Update()
 {
     
     if (FollowMouse || Input.GetMouseButton(0))
     {
         _target = Camera.ScreenToWorldPoint(Input.mousePosition);
         
         _target.y = 0;
     }
     
     var delta = ShipSpeed * Time.deltaTime;
     if (ShipAccelerates)
     {
         delta *= Vector3.Distance(transform.position, _target);
     }
    
     transform.position = Vector3.MoveTowards(transform.position, _target, delta);

    

 }

thank you so much for helping!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cherno · Jun 27, 2016 at 07:32 PM

 public Vector3 lookAtTargetPos;

 void Update() {
      lookAtTargetPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);

          if(lookAtTargetPos != Vector3.zero) {
             Quaternion rot = Quaternion.LookRotation(transform.position - lookAtTargetPos, Vector3.up);
             transform.rotation = rot;
             transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
         }

}

Comment
Add comment · Show 7 · 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 mnmwert · Jun 28, 2016 at 07:22 PM 0
Share

Thanks for answering. I had to modify Camera.main.ScreenToWorldPoint(Input.mousePosition);

to

Camera.ScreenToWorldPoint(Input.mousePosition);

because my camera is attached to my player (also note it is orthographic). When I changed the line of code my player rotated constantly very rapidly though at least it was only on the Y axis. Thank you again for answering and I hope this problem is solved quickly!

avatar image mnmwert · Jun 28, 2016 at 07:25 PM 0
Share

I changed one thing and it all worked!

Quaternion rot = Quaternion.LookRotation(transform.position - lookAtTargetPos, Vector3.up);

to

Quaternion rot = Quaternion.LookRotation(transform.position + lookAtTargetPos, Vector3.up);

Yay!!!!!!!!!

avatar image mnmwert · Jun 28, 2016 at 07:28 PM 0
Share

There is one problem though. It will not turn when the mouse is behind the player and then when the mouse is in certain parts of the screen it will spin very rapidly.

avatar image mnmwert · Jun 28, 2016 at 07:39 PM 0
Share

So I took my player camera off the player and it works but it does not turn fast enough. also I have a particle emitter as an engine and that won't rotate with the player. I will also need it to tilt on the Z axis as it turns to signify a tilting action. (sorry i didn't say it at first but i just wanted to get through that)

avatar image Cherno mnmwert · Jun 29, 2016 at 08:17 AM 0
Share

Set the ParticleSystem's simulation space to local. Why it won't fast enough is beyond my understanding since the rotation is applied every frame, which is as fast as possible.

avatar image mnmwert Cherno · Jun 29, 2016 at 07:10 PM 0
Share

the simulation space actually is set to local. what I mean by rotate fast enough is that it needs to look like it is flying almost. (except it is in space)

Show more comments

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

62 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

Related Questions

Temporarily turn off the relative rotation from parent in my script while keeping the relative position updated? 1 Answer

Synchronising the rotation around Y axis of two objects 1 Answer

Rotation drift with transform.eulerAngles 0 Answers

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

Object won't rotate 2 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