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 Serreas · Dec 29, 2013 at 02:12 AM · rotate objectvector3.slerp

rotating road gameobject towards mouse position

So i made this code here which lets me lengthen a plane with a road texture on it to any length i want (and still maintain proper texture tiling) based on distance between two mouse positions. What I'm trying to do now is to rotate my road (around its pivot basically which i set up to be on one end point of the road object) towards wherever the mouse position or mouse coordinates are I would like it to be similar to the way you place roads in simcity where you click on the ground and rotate the road in any direction you want. I just cant seem to figure out how to do it nothing i tried worked so far. Maybe someone can help me out. I hope i explained it clearly enough.

 public class Roadplacement : MonoBehaviour 
 {
     public GameObject Road;
     public GameObject RoadPrefab;
     private Vector3 mousePos1;
     private Vector3 mousePos2;
     private float RoadLength;
     private float RoadScale;
 
 
     // Use this for initialization
     void Start () 
     {
 
     }
 
     void OnGUI()
     {
 
     }
 
     // Update is called once per frame
     void Update () 
     {
 
         Vector3 m = Input.mousePosition;
         m = new Vector3(m.x,m.y,transform.position.y);
         Vector3 p = Camera.main.ScreenToWorldPoint(m);
         Vector3 mousecoord = new Vector3(p.x,0,p.z);
 
         //Road.transform.eulerAngles = new Vector3(0,Road.transform.eulerAngles.y,0);
         //Debug.Log(Road.transform.eulerAngles);
 
         if(Input.GetMouseButtonDown(0))
         {
             mousePos1 = new Vector3(p.x,0,p.z);
             //Debug.Log(mousePos1);
 
         }
 
         if(Input.GetMouseButton(0))
         {
 
             mousePos2 = new Vector3(p.x,0,p.z);
             Vector3 offset = (new Vector3(0,0,mousePos2.z)) - (new Vector3(0,0,mousePos1.z));
             RoadLength = offset.magnitude;
             RoadScale = RoadLength * 0.1f;
 
             if(offset.z > 0)
             {
                 Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,RoadScale);
             }
 
             if(offset.z < 0)
             {
                 Road.transform.localScale = new Vector3(Road.transform.localScale.x,Road.transform.localScale.y,-RoadScale);
             }
 
             RoadPrefab.renderer.material.mainTextureScale = new Vector2(1,Road.transform.localScale.z *2);
 
             Debug.Log(offset);
             Debug.Log(RoadLength);
             Debug.Log(RoadScale);
             Debug.Log(mousePos2);
             Debug.Log(Road.transform.localScale);
         }
 
     
     }
 
 }
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
Best Answer

Answer by robertbu · Dec 29, 2013 at 02:48 AM

It appears this is a top-down view and you are scaling the objects on the 'z' axis? If so, you can calculate the rotation like:

 Road.transform.rotation = Quaternion.FromToRotation(Vector3.forward, mousePos2 - mousePos1);
Comment
Add comment · Show 3 · 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 Serreas · Dec 29, 2013 at 03:10 AM 0
Share

oh sorry i forgot to mention that i can rotate my camera 360 degrees as well as move it about with the wasd keys in pretty much any direction. Im basically trying to rotate the gameobject around its y axis towards the mouse cursor. Its basically going to be a city building type of game.

avatar image robertbu · Dec 29, 2013 at 03:29 AM 0
Share

If the camera can rotate, then you need to convert mousePos1 and mousePos2 into world coordinates before you calculate the rotation. If the camera is parallel to the XZ plane, you can use Camera.ScreenToWorldPoint(). If the camera is at an angle then some form of raycast will work. I recommend using Unity's mathematical Plane class and using Plane.Raycast() to find the world points. The calculation for the rotation will be as I've indicated only using world positions.

avatar image Serreas · Dec 29, 2013 at 07:40 PM 0
Share

Finally figured it out with your help thanks so much! I created a new plane and a ray which emitted from the mouse position and used the following if statement code for the rotation. I used quaternion.slerp as it give me a smooth rotation.

 if (roadPlane.Raycast (ray, out hitdist))
             {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
 
             }

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

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

How to use Lerp(Slerp) to rotate on the Z Axis. 3 Answers

Move and Rotate player in circular motion? 2 Answers

rotate object around another object 1:1 1 Answer

How to rotate an object with animation? 0 Answers

I need help with my character's rotation 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