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 SpaceSloth · Oct 12, 2012 at 03:09 AM · positionmousemove

Move object towards mouse position

Hey people ! I'm quite new at unity, so I'm trying out some scripting.

Basically my intent is to have a spaceship controlled with the WASD keys and having it always pointing to the position of the mouse. All of that in 3D isometric view.

Now I managed to get my spaceship moving with the WASD keys easily enough, and I managed to make it always look at the mouse, in two sepatate scripts.

My problem is that the WASD movements are relative to the position of the spaceship in the world, and not relative to the mouse's position.

Basically, I want the spaceship's movements to always be depending on the mouse. Forward should mean towards the mouse, not just towards a general direction.

Here's my code

 using UnityEngine;
 using System.Collections;
 
 public class LookAt : MonoBehaviour {
 
 
     public float speed;
     RaycastHit hit;
     Plane ground;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         ground = new Plane(Vector3.up, transform.position);
         
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         float dist = 0.0f;
                 
         if(ground.Raycast(ray,out dist)){
             Vector3 clickPoint = new Vector3(ray.GetPoint(dist).x, transform.position.y, ray.GetPoint(dist).z);
             
             Quaternion targetRotation = Quaternion.LookRotation(clickPoint - transform.position);
             
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         
     }
 }
 }
 



I looked around the forums, but couldn't find a solution, wat do?

Thank you for your answer in advance, preferably in C# :)

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

Answer by sparkzbarca · Oct 12, 2012 at 06:39 AM

gameobject.transform.forward

all objects have a position all objects with a transform have a transform object

all transform objects have .forward function which is forward RELATIVE TO there own local axis.

for a first person ship you want to attach the script to the camera and do

this.transform.forward

alternately you can do

Camera.main.transform.forward from within any script assuming your camera is tagged as main.

im just confused as to whether this is a first person or 3rd person view

first person you want to move towards camera.transform.forward

third person you can simply rotate the ship towards the pointer as you have said you already did then use the ships forward that is.

ship.transform.position += ship.transform.forward speed time.deltatime

if you need any further help implementing feel free to ask in comment

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 sparkzbarca · Oct 12, 2012 at 03:40 AM 0
Share

by the way if you want to use physics in this space shooter, that is have collisions and stuff you need to attach a rigidbody and apply forces to move the object.

transform functions which is any transform.x are teleportations. They instantly move the ship to whatever position you say with no attempt to check if anything is in the way. there is no along the way, they teleport, even teleporting inside other objects.

rigidbody.x rigidbody functions apply forces and use Nvidia Physix engine to perform collision tests etc. They aren't teleporting.

avatar image SpaceSloth · Oct 12, 2012 at 10:15 AM 0
Share

Thank you for your help!

The view is in 3d person, top down but not entirely. With

ship.transform.position += ship.transform.forward speed time.deltatime

the spaceship moves in the direction of the mouse, but it does so automatically.

Now I implemented

if(Input.GetButton("Forward")){ spaceship.transform.position += spaceship.transform.forward moveSpeed Time.deltaTime; }

and the same * -1 for Backwards, my problem now is that it works forwards and backwards, but I don't know which transform to use to make it go sideways, relative to the mouse.

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

mouse clicking on the plane and drag to other position 1 Answer

Problem with Alt + Middle Click Drag for Camera (Windows) 3 Answers

Prevents a mouse click on interface to trigger a click to move script 1 Answer

Mouse position on 4.6 Canvas 0 Answers

Drag object with mouse in diagonal direction 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