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
0
Question by arucart · Mar 23, 2013 at 09:04 PM · axisshooterlockdowntop

Top down camera player rotating

So I've got a script which makes the player rotate where the mouse is. I'm just wondering if theres a way to lock an axis. Because when the mouse goes over the player model the model rotates and faces straight up.

 var speed : float = 20.0;
 var rotatespeed : float = 2.0;
 
 function Update() {
 
     var controller : CharacterController = GetComponent(CharacterController);
     transform.Rotate(0,Input.GetAxis("Horizontal") * rotatespeed, 0);
     
     var forward : Vector3 = transform.TransformDirection(Vector3.forward);
     var currentspeed : float = speed* Input.GetAxis("Vertical");
     controller.SimpleMove(forward * currentspeed);
     
     var position = Input.mousePosition;
     var newposition = Vector3(position.x, position.y, camera.main.transform.position.y- transform.position.y);
     var lastposition = camera.main.ScreenToWorldPoint(newposition);
     transform.LookAt(lastposition);
     
 }
 
Comment
Add comment · Show 3
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 robertbu · Mar 23, 2013 at 09:06 PM 0
Share

Is this a 2D game? Is the camera position fixed?

avatar image arucart · Mar 23, 2013 at 09:22 PM 0
Share

Yeah its 2D, I've had the problem with fixed camera and smooth follow

avatar image ExTheSea · May 06, 2013 at 06:42 PM 0
Share

Did the answer from robertbu solve your problem? If so please accept the answer to mark your question as solved. If it didn't tell us how it didn't.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 23, 2013 at 09:58 PM

This is a bit of untested modification to your code. Instead of forward, it uses right, so the right side of your object should track the mouse. If you need 'Z' to track, apply this script to an empty game object and then make the visible game object a child with the correct rotation.

 #pragma strict
 
 var speed : float = 20.0;
 var rotatespeed : float = 2.0;
  
 function Update() {
  
     var controller : CharacterController = GetComponent(CharacterController);
     transform.Rotate(0,Input.GetAxis("Horizontal") * rotatespeed, 0);
  
     var forward : Vector3 = transform.TransformDirection(Vector3.forward);
     var currentspeed : float = speed* Input.GetAxis("Vertical");
     controller.SimpleMove(forward * currentspeed);
  
     var position = Input.mousePosition;
     var newposition = Vector3(position.x, position.y, camera.main.transform.position.y- transform.position.y);
     var lastposition = camera.main.ScreenToWorldPoint(newposition) - transform.position;
     var angle : float = Mathf.Atan2(lastposition.z, lastposition.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
 }
Comment
Add comment · Show 2 · 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 arucart · Mar 23, 2013 at 10:53 PM 0
Share

I get this error trying to use it "Assets/Scripts/Player.js(18,37): BCE0005: $$anonymous$$ identifier: 'lookPos'."

avatar image robertbu · Mar 23, 2013 at 11:01 PM 0
Share

Should have been 'lastposition'. Code was copied and modified from elsewhere.

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

11 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

Related Questions

Top Down Shooter Help 1 Answer

How To Get Object To Aim at Mouse in Top Down 3D Shooter 4 Answers

C# LookAt Mouse 2D 2 Answers

Lock a rotation 1 Answer

Lock the y axis (on an object that's not a rigid body) c# 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