Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by bryan · Oct 04, 2010 at 08:19 PM · 2dgameobjectmouserotatepoint

Rotate a gameobject(player)on the y axis towards mouse position in c#

been looking for a way to do this but havent found it in c# pleas god some one help us.

this is what we have so far for are character

using UnityEngine; using System.Collections;

public class carl : MonoBehaviour { public GameObject ProjectilePrefab;

public float PlayerSpeed;

// Update is called once per frame void Update () {

 // Amout to move
 float inputX = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
 float inputY = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;


 // Move the Player
 transform.Translate(Vector3.forward * inputX) ;
 transform.Translate(Vector3.left * inputY) ;





 if (Input.GetKey("space"))
 {
         //Fire projectile
         Instantiate(ProjectilePrefab, transform.position, Quaternion.identity);


 }



}

}

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 Anthonyknowles · Mar 24, 2015 at 02:27 PM 0
Share

Im trying the exact same thing here - did this ever get sorted?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IJM · Oct 04, 2010 at 08:33 PM

You can do this:

  Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
  transform.LookAt(MouseWorldPosition);
  transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));

Call this from your player script.

Comment
Add comment · Show 5 · 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 bryan · Oct 04, 2010 at 11:57 PM 0
Share

cool thanks but now i get an error '$$anonymous$$ainCamera' does not exist in the current context

any ideas

avatar image bryan · Oct 05, 2010 at 12:05 AM 0
Share

got it working but the player spins out of control and isnt looking at the mouse pointer position.

avatar image IJM · Oct 05, 2010 at 12:18 AM 0
Share

Did you call this from your players script?

avatar image bryan · Oct 05, 2010 at 01:13 AM 0
Share

yeah we added it in and the player immediately turned 90 deg and would run around and rotate, just not in the direction of the mouse position

avatar image IJM · Oct 05, 2010 at 12:13 PM 0
Share

You are doin something wrong. Can you put the whole player code down, with the implementation of this? (as a answe)

avatar image
0

Answer by bryan · Oct 05, 2010 at 04:19 PM

using UnityEngine; using System.Collections;

public class carl : MonoBehaviour { public GameObject ProjectilePrefab; public GameObject MainCamera;
public float PlayerSpeed; public float QuaternionSpeed;

// Update is called once per frame void Update () {

 Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
 transform.LookAt(MouseWorldPosition);
 transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));

 // Amout to move
 float inputX = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
 float inputY = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;


 // Move the Player
 transform.Translate(Vector3.forward * inputX) ;
 transform.Translate(Vector3.left * inputY) ;






 if (Input.GetKey("space"))
 {
         //Fire projectile
         Instantiate(ProjectilePrefab, transform.position,Quaternion.identity);


 }



}

} there it is, he moves, and rotates kinda randomly, but does not rotate to look at mouse postition

Comment
Add comment · Show 4 · 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 bryan · Oct 05, 2010 at 04:20 PM 0
Share

it lost the top part here using UnityEngine; using System.Collections;

public class carl : $$anonymous$$onoBehaviour { public GameObject ProjectilePrefab; public GameObject $$anonymous$$ainCamera;
public float PlayerSpeed; public float QuaternionSpeed;

avatar image IJM · Oct 06, 2010 at 05:36 PM 0
Share

You need something like this in your Start() function: $$anonymous$$ainCamera = GameObject.Find("CameraName"); You need to know what is the name of your camera in Editor. (CameraName)

avatar image speed.fire · May 23, 2013 at 05:53 PM 0
Share

I think it's Camera.mainCamera, not $$anonymous$$ainCamera.camera. :)

avatar image HenryChinaski speed.fire · Aug 14, 2016 at 08:06 PM 0
Share

It is Camera.main

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D rotate object towards mouse? 1 Answer

Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer

How to make a 2D character points his gun to the mouse position? 2 Answers

C# Rotate GameObject at Other Point other than Center 3 Answers

Horizontal gravity on One gameobject 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