Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make a rotating line(capsule) always look at a sphere on the y-axis. (C#)

Discussion in 'Scripting' started by Stakior, Nov 4, 2014.

  1. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Hi fellow Unity-User :D

    I'm having a Problem with a task.

    I have two points(Spheres) with a line(capsule) and another point with a line.
    I called them PointA, PointB, PointC, Line and Line 2.
    Line goes through PointA and PointB and PointB and the first Line are rotating around PointA.
    Now I want Line2 to rotate with PointB around PointA..... but I also want Line2 to never leave the not moving PointC.

    Do you guys have any ideas ?
    If something isn't clear I'll try to do my best to explain it to you :D

    Thanks for help in advance

    Stakior

    upload_2014-11-4_11-33-1.png

    I hope you can see the pic :D

    PointC,Line2,PointB,Line and PontA (from left to right)
     
    Last edited: Nov 4, 2014
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    A picture of this problem would be great, as yes, it isn't quite clear enough for me... :D
     
  3. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    How can I get a pic in here ? .-.
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh dear... :D

    Anyway, when posting, use the "upload a file" button, select your picture, and once it uploads, click either "thumbnail" or "full image" on the upload thing at the bottom of your posting box, and it should appear where your text cursor is in the text area.
     
  5. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    I think I did it :D
     
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I don't see anything... :D
     
  7. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    For real ? I edited my thread and now it's at the top .-.
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh, forgot to check first post! *godzilla-facepalm*
    This is definitely confusing to do though...

    What you seem to be trying to do is rotate one point, and have all the others, along with the lines, go round that point and Point C, am I correct? o_O
     
  9. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh wait!

    I realized something: try using transform.RotateAround(point, angle) with the moving points, and transform.LookAt(point) to have the point's line still point at point C. this should work with both lines.

    At least that is how I think it would work... :D
     
  10. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    I tried transform.LookAt but I cant get Line2 to look at PointC all the time .-.
     
  11. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    and than I Need to change it to look at it with its y-axis too .-.
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok...

    Where is line2 cast from? o_O I know Line1 is cast in point A.

    And what do you mean by "look at it with the Y-axis too?" :D If you mean facing the Y axis towards Point C, I am having the same problem too, but in my case, it's so my characters face feet-first towards a distant planet... ;)
     
    Last edited: Nov 4, 2014
  13. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Do you mean the middle of the "Line" cuz like I said it's just a capsule with a scale of (0.1,9999,0.1) :D

    I you mean the middle of it, it's in PointB (the rotating one)
     
  14. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Well, then we are kinda in the same boat :D Damn .-.
     
  15. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
  16. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
  17. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok... I just got my up vectors pointing correctly.

    basically, have your sphere point like this (Note that I ripped this straight out of my code, so it's in JS):
    Code (csharp):
    1. gravityVector = <the point you want to point to> - enemy.transform.position;
    2.         <object to point with>.transform.up = -gravityVector;
    If the object ends up pointing away from it instead though, remove the negative operator on the second line. (I had to leave it in, as my characters heads were pointing towards the origin! :D)
     
  18. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Still doesn't work .-. I think I can't figure out how to use this code ._.
     
  19. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    What's up with the gravityVector ? it doesn't excist in C# .-.
     
  20. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Where's your code? I can help better if you post it. :)
     
  21. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh, and you have to declare "gravityVector" somewhere in your code as a vector3, sorry for not cleaning that up! :D
     
  22. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    So the gravityVector is the Point I want it to Point to ? But what's enemy then ?
     
  23. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    the gravityVector is the point you want to point to, minus the object the script's attached to's position. after generating gravityVector, you then set the up direction of your object to the gravity vector. (If you see weird results, try reversing the vector after generating and before setting the up direction)
     
  24. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44




    using UnityEngine;
    using System.Collections;

    public class Linie2 : MonoBehaviour {
    public Spin sc;
    public Vector3 gravityVector = new Vector3(-12,5,5);
    // Use this for initialization
    void Start () {
    GameObject capsule = GameObject.CreatePrimitive (PrimitiveType.Capsule);
    capsule.transform.position = new Vector3(-5f,-2f,7f);
    capsule.transform.eulerAngles = new Vector3(46.09098f, 254.0546f, 180f);
    capsule.transform.localScale = new Vector3(0.1f,9999f, 0.1f);
    capsule.AddComponent ("Color.red");
    capsule.renderer.material.color = Color.red;
    sc = capsule.AddComponent ("Spin") as Spin;
    capsule.transform.up =- gravityVector;
    }
    }

    I did it like this now but the line2 still leaves PointC :/
     
    Last edited: Nov 5, 2014
  25. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Try changing this:
    Code (csharp):
    1. capsule.transform.up = -GravityVector
    to this:
    Code (csharp):
    1. capsule.transform.up = GravityVector
     
  26. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Also, I re-read one fo your posts, the enemy variable was from my own code, (oops!) so just replace that with your sphere/capsule's transform
     
  27. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    I tried this now and it still doesn't work .-.

    using UnityEngine;
    using System.Collections;
    public class Linie2 : MonoBehaviour {
    public Spin sc;
    public Vector3 gravityVector = new Vector3(-12,5,5);
    public Vector3 enemy = new Vector3(-12,5,5);
    // Use this for initialization
    void Start () {
    GameObject capsule = GameObject.CreatePrimitive (PrimitiveType.Capsule);
    capsule.transform.position = new Vector3(-5f,-2f,7f);
    capsule.transform.eulerAngles = new Vector3(46.09098f, 254.0546f, 180f);
    capsule.transform.localScale = new Vector3(0.1f,9999f, 0.1f);
    capsule.AddComponent ("Color.red");
    capsule.renderer.material.color = Color.red;
    sc = capsule.AddComponent ("Spin") as Spin;
    gravityVector =- capsule.transform.position;
    capsule.transform.up = gravityVector;
    }
    }
     
  28. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    found your problem
    this line:
    Code (csharp):
    1. gravityVector =- capsule.transform.position;
    is missing the origin of your line's center point.
    Instead, use this:
    Code (csharp):
    1. gravityVector = <point to look at> - capsule.transform.position;
    Hopefully, that works.
     
  29. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    Yeah :D that's better it starts with looking at it but than doesn't after the first frame, but I need it to look at it at all time .-. I tried this but it givest me Errors

    using UnityEngine;
    using System.Collections;
    public class Linie2 : MonoBehaviour {
    public Spin sc;
    public Vector3 gravityVector = new Vector3(-12,5,5);
    public Vector3 enemy = new Vector3(-12,5,5);
    // Use this for initialization
    void Start () {
    GameObject capsule = GameObject.CreatePrimitive (PrimitiveType.Capsule);
    capsule.transform.position = new Vector3(-5f,-2f,7f);
    capsule.transform.eulerAngles = new Vector3(46.09098f, 254.0546f, 180f);
    capsule.transform.localScale = new Vector3(0.1f,9999f, 0.1f);
    capsule.AddComponent ("Color.red");
    capsule.renderer.material.color = Color.red;
    sc = capsule.AddComponent ("Spin") as Spin;
    }
    void Update() {
    gravityVector = new Vector3(-12,5,5)- capsule.transform.position;
    capsule.transform.up = gravityVector;
    }
    }

    it says that the name 'capsule' doesn't exist in the context (19,55) and (20,17)
     
  30. Stakior

    Stakior

    Joined:
    Sep 23, 2014
    Posts:
    44
    I got it :D

    This feels so damn good xD

    I just made it to a new script, changed capsule to gameObject and added the script to the Linie2 script and now everything works. Yay :)

    Thanks for your help :D I owe you one :)
     
  31. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    You're welcome! :) So I guess you realized your capsule was declared locally, am I not right? :D

    Would also like to see a video of this, as being able to see it in action would be interesting. :)