• 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 looneygag · Nov 18, 2013 at 11:51 PM · 2draycastmouse

Getting a character to fly(follow cursor in 2D) in Unity4.3

I've created a script based on the look at mouse script in the UnityWiki. I attached it to my player and when I run the script he just sits there, no reaction. Any ideas? I'm fairly noobish so it's probably something simple. Thanks! Code:

     using UnityEngine;
     using System.Collections;
 
     public class Flying : MonoBehaviour {
 
     public float speed;
 
     // Use this for initialization
     void Start () 
     {
 
     }
 
     // Update is called once per frame
     void FixedUpdate () 
     {
     if (Input.GetKeyDown(KeyCode.LeftShift))
         {   
         // Generate a plane that intersects the transform's position with an upwards normal.
         Plane playerPlane = new Plane(Vector3.up, transform.position);
 
         //Generate a ray from cursor position
         Ray 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.
 
         float hitdist = 0.0f;
 
             if (playerPlane.Raycast (ray, out hitdist))
                 {
                     // Get the point along the ray that hits the calculated distance
                     Vector3 targetPoint = ray.GetPoint(hitdist);
 
                     //determine the target rotation. This is the rotation if the transform looks at the target
                     Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
                     // Smoothly rotate towards the target point.
                     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time);
 
                     //Move forward
                     transform.position = transform.forward *speed *Time.deltaTime;
                 }
         }
 
 }
 }
Comment
Add comment · Show 2
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 cmpgk1024 · Nov 19, 2013 at 12:02 AM 0
Share

This code seems to be for a 3d game.

avatar image looneygag · Nov 19, 2013 at 12:31 AM 0
Share

Damn, I'm really new and only came to Unity for the 2d support. Could you help me translate it to 2d?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 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

Related Questions

BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers

how can i Find an object height, while click on that object? 1 Answer

object rotates toward mouse? 2D Top Gameplay 6 Answers

How to make a 2d mouse look script 3 Answers

2D jump with Raycast 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges