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 Max-TheArgentine · Sep 02, 2014 at 10:19 PM · rotateroad

How to Rotate a "Street" correctly

Hi guys, i am working on a Sub-System, a road creation in a simCity way. i am in a phase of experimentation, i know the basics, so here is my question. i am using a function named mousePosition,with raycasting. i create a mesh dinamicaly(4 vertices), The idea is simple, i made 1 click and CreateRoad is called. The center of the mesh is exactly where the mouse was clicked. the road lenght is 4. Alpha(4Pix)-------Beta(0pix)
imagine the road with both extremes Alpha and Beta. the center is on B. i want to rotate Alpha toward the mouse location. point Beta must be fixed and A always have to be looking at mouse location. My problem is that my road rotates with Beta looking the mouse location, not Alpha.The road rotates complete, not with a fixed point. i have tried to add a GameEmpty like a parent but i cant separate the Transform from the road mesh becouse there is no option to do this from Unity. i have tried a lot of codes and i dont get it to work, i have readed a lot of post too. here is the complete script with comments. i would love to know if somebody knows how to do it correctly. Thanks a lot, and hope you understand my question. Image beneath.


using UnityEngine; using System.Collections;

public class GroundClick : MonoBehaviour {

 public GameObject prefabRoad;
 public GameObject nodeRotation;
 public GameObject prefabNode;


 float lenght;
 Vector3 roadEnd;
 Vector3 roadStart;
 Vector3 lookAtRoad;
 float width;
 Mesh mesh;
 bool boton;
 GameObject road;
 RaycastHit hitinfo ;
 Ray ray;
 MeshFilter mesh_filter;
 int creandoCalle;  //si vale 1 se dio un click y se esta posicionando la calle
                     // si vale 2 se dio el segundo click y se lo pone definitivamente
                     //si vale cero no se hizo click sobre crear calle

 
 
 
 void Start(){
     creandoCalle=0;
 }

 
 
 void Update () {
     
         if(Input.GetMouseButtonDown(0))
         {
             getClickLocation(out roadStart);
             createRoad(roadStart);

             creandoCalle=1;
         }

         if(creandoCalle==1){
             rotate();
         }
     
 }


 void rotate()
 {

     getClickLocation(out lookAtRoad); //obtain the mouse position and saves it on LookAtRoad
     Debug.Log(lookAtRoad);

             //Quaternion rot=Quaternion.LookRotation(lookAtRoad);
             //rot*= Quaternion.Euler(new Vector3(0,0,0));
             //road.transform.rotation=Quaternion.Slerp(road.transform.rotation,rot,Time.deltaTime*100);
             
             //road.transform.rotation= Quaternion.RotateTowards(road.transform.rotation,rot,1);
             //road.transform.rotation=Quaternion.FromToRotation(road.transform.position,lookAtRoad.normalized);
             //road.transform.RotateAround(road.transform.forward*new Vector3(0,0,4),new Vector3(0,0,1),Quaternion.Angle(road.transform.rotation,))
 road.transform.LookAt(lookAtRoad);
             //road.transform.rotation=Quaternion.FromToRotation(road.transform.position,lookAtRoad);
             //road.transform.Rotate
 }

 void TerminarTramoDeCalle ()
 {

 }
 
 bool getClickLocation(out Vector3 point)
 {
     ray=Camera.main.ScreenPointToRay(Input.mousePosition);
     hitinfo =new RaycastHit();
     if(collider.Raycast(ray,out hitinfo, Mathf.Infinity)){
         point =hitinfo.point;
         return true;
     }
     point= Vector3.zero;
     return false;
 }
 
 
 void createRoad(Vector3 roadStart){
     
     road= Instantiate(prefabRoad) as GameObject;
     
     road.transform.position =  roadStart+ new Vector3(0,0.01f,0);
     //road.transform.rotation= Quaternion.FromToRotation(roadStart, roadEnd);
     width=1;
     lenght=4;
     
     Vector3[] vertices = new Vector3[]
     {
         new Vector3( 1, 0, 1),
         new Vector3( 1, 0, -5),
         new Vector3(-1, 0, 1),
         new Vector3(-1, 0, -5),
     };

     int[] triangles = new int[]
     {
         0, 1, 2,
         2, 1, 3,
     };
     Vector2[] uv = new Vector2[]
     {
         new Vector2(1, 1),
         new Vector2(1, 0),
         new Vector2(0, 1),
         new Vector2(0, 0),
     };

     Vector3[] normals={
         Vector3.up,
         Vector3.up,
         Vector3.up,
         Vector3.up
             
     };
     
     mesh= new Mesh();
     mesh.vertices=vertices;
     mesh.triangles=triangles;
     mesh.uv=uv;
     
     mesh.normals=normals;
     
     mesh_filter=road.GetComponent<MeshFilter>();
     mesh_filter.mesh=mesh;

 }
 
 

}


i have tried a lots of code that doesnt work properly. alt text

example.png (16.9 kB)
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 Max-TheArgentine · Sep 03, 2014 at 01:26 PM

Well, i solved the rotation problem simply by changinf the vertices of the mesh by: new Vector3( 1, 0, 5), new Vector3( 1, 0, -1), new Vector3(-1, 0, 5), new Vector3(-1, 0, -1),


Now the problem Off topic is that when i overfly the mesh zone, the mesh goes under the terraing. maybe is the LookAt function problem, i will check and then if i cant solve it post my new questions. thanks

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

How to rotate an object so it always faces up? 1 Answer

rotate animation 2 Answers

Is it possible to rotate uv coordinates in a script? 1 Answer

Rotate An Object Toward The Direction The Joystick Is Pressed 3 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