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 Tyler 2 · Apr 25, 2011 at 12:20 AM · mousecursorlook

Look at the mouse?

Hello. In this game, the space ship rotates to look at the mouse cursor. How can I do this, but only along one axis? Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Meater6 · Apr 25, 2011 at 12:38 AM

Try this:

// LookAtMouse will cause an object to rotate toward the cursor, along the y axis. // // To use, drop on an object that should always look toward the mouse cursor. // Change the speed value to alter how quickly the object rotates toward the mouse.

// speed is the rate at which the object will rotate var speed = 4.0;

function Update () { // Generate a plane that intersects the transform's position with an upwards normal. var playerPlane = new Plane(Vector3.up, transform.position);

 // Generate a ray from the cursor position
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

 // Determine the point where the cursor ray intersects the plane.
 // This will be the point that the object must look towards to be looking at the mouse.
 // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
 //   then find the point along that ray that meets that distance.  This will be the point
 //   to look at.
 var hitdist = 0.0;
 // If the ray is parallel to the plane, Raycast will return false.
 if (playerPlane.Raycast (ray, hitdist)) {
     // Get the point along the ray that hits the calculated distance.
     var targetPoint = ray.GetPoint(hitdist);

     // Determine the target rotation.  This is the rotation if the transform looks at the target point.
     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

     // Smoothly rotate towards the target point.
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
 }

}

I got it from Unity Wiki, and I want to make sure the person who made it gets the credit for this script. I tried to use a link, but it didn't seem to work. Hope this helps.

EDIT:

I made this script here that makes the object it's on look at where the mouse intersects. This will do for the rotation of the spaceship, as long as there is a invisible plane in the background (you can also do it through scripting, without the invisible plane) . But the movement part is much more complicated. I think it might work like this, the ship moves toward the x,y value of the mouse intersect point, while keeping its z position the same. I tested this method and it seems like the movement in the game.

SpaceshipScript:

var posZ : float = 0; var smooth : float = 0.1;

function Update () { var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;

 if(Physics.Raycast(ray,hit)){
     transform.LookAt(hit.point);
     transform.position = Vector3.Lerp(transform.position,hit.point,smooth);
 }
 transform.rotation.z = 0;
 transform.position.z = posZ;

}

I hope this answers your question.

Comment
Add comment · Show 6 · 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 Tyler 2 · Apr 25, 2011 at 12:54 AM 0
Share

Actually, I already tried that script, and I couldn't get it to work. I tried to replace the "Vector3.up" to "Vector3.right" and nothing happens and when I try to change it to "Vector3.forward" and the script goes haywire (I cant really explain it, the object starts rotating along multiple axes).

avatar image Meater6 · Apr 25, 2011 at 01:15 AM 0
Share

It depends on what you want. I would like it if you added more information to your question, for example what axis. For the game you displayed, this script would not work. I tried changing the Vector3, and it worked fine.

avatar image Tyler 2 · Apr 25, 2011 at 01:40 AM 0
Share

I want to make something exactly like the game that I linked to in my question. I want the ship to be facing forward (along the world z axis) and I want the ship to look towards the cursor, rotating along the x and y axes. The reason I asked how to rotate along one axis only is to prevent the object rolling around the z axis (when I use a normal "lookAt" code, the object will rotate around the z axis to face the object). $$anonymous$$y plan is to have an empty object and rotate it along the x axis, put the ship in the game object, and then rotate the ship around the y axis (so there will be no z rotation)

avatar image Tyler 2 · Apr 25, 2011 at 01:41 AM 0
Share

If this script would not work, do you have any ideas on how I would accomplish what I want?

avatar image Meater6 · Apr 25, 2011 at 04:39 AM 0
Share

Ok, your right this script wouldn't work for that. I would try making a plane in the background, use the same type of raycast as in the script above, and use a smooth look at to the hit point, while locking the z axis. If the raycast hits a asteroid, look at that ins$$anonymous$$d, Though, the game that you present does not lock the z axis. It seems to smooth look at the point, then once it has reached the correct position (You define what that position is) it then resets it self to 0 rotation on the z axis. I will try to find a better explanation to this.

Show more comments
avatar image
0

Answer by Aydan · Apr 25, 2011 at 12:57 AM

add this script to your camera then set the fpc to the object you want to rotate.

private var worldPos : Vector3; private var mouseX : int; private var mouseY : int; private var cameraDif : int; var fpc : GameObject;

function Start () {

 //determines how far down the ScreenToWorldPoint is from the camera position
 //it's calculated [height of camera] - [height of pivot point of character]
 //this is to ensure the character only rotates (via LookAt) along rotation.x and doesn't look up or down
 cameraDif = camera.transform.position.y - fpc.transform.position.y;


}

function Update () {

 mouseX = Input.mousePosition.x;
 mouseY = Input.mousePosition.y;


 //this takes your current camera and defines the world position where your mouse cursor is at the height of your character -->translates your onscreen position of mouse into world coordinates
 worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));

 fpc.transform.LookAt(worldPos);

}

this is for a top down game so it may come in helpful

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 Tyler 2 · Apr 25, 2011 at 01:03 AM 0
Share

Is there any way to use this script and have the object positioned at some other place that 1 on the Y axis?

avatar image Aydan · Apr 25, 2011 at 01:04 AM 0
Share

this is for a top down game so maybe if you changed the axis you could get the what your looking for.

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

No one has followed this question yet.

Related Questions

Movement along X and Z axis... 2 Answers

error CS8025: Parsing error 1 Answer

How to make something happen when mouse on side of screen? 1 Answer

Unity 3D Screen.lockCursor Problems 2 Answers

How to convert the MouseLook(Script) to a java script. 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