# Discussion Rotate a capsule between two points

Discussion in 'Getting Started' started by FatinFardaous, Jan 24, 2023.

1. ### FatinFardaous

Joined:
Nov 9, 2022
Posts:
6
Let's say there are two point A and B. A capsule object need to go from point A to B.
I already did the scaling part but can't seems to get how the rotation is working.
My code part for rotation is
pointA is (p1x,p1y,p1z)
pointB is (p2x,p2y,p2z)

Code (CSharp):
1.         float dirx = (p1x + p2x) / 2 - p1x;
2.         float diry = (p1y + p2y) / 2 - p1y;
3.         float dirz = (p1z + p2z) / 2 - p1z;
4.
5.         float len = (float)Math.Sqrt(dirx * dirx + diry * diry + dirz * dirz);
6.         float x_angle = (float)Math.Acos(dirx / len);
7.         x_angle= x_angle * 180 / 3.1415f;
8.
9.         float y_angle = (float)Math.Acos(diry / len);
10.         y_angle = y_angle * 180 / 3.1415f;
11.
12.         float z_angle = (float)Math.Acos(dirz / len);
13.         z_angle = z_angle * 180 / 3.1415f;
14.         c1.transform.eulerAngles = new Vector3(x_angle-90,y_angle, z_angle-90);

2. ### DevDunk

Joined:
Feb 13, 2020
Posts:
3,470
I suggest using included methods like transform.Rotate(Around) or Quaternion.Lerp

AngryProgrammer likes this.
3. ### AngryProgrammer

Joined:
Jun 4, 2019
Posts:
286
Dude, read the Unity manual or do a tutorial. Don't reinvent the wheel. You use an engine so you don't have to do things like that.

To move stuff you use Translate method, to rotate you use Rotate method. To add some physics to movement you have AddForce.

DevDunk likes this.
4. ### FatinFardaous

Joined:
Nov 9, 2022
Posts:
6
Brother I am doing a project where python script is sending me coordinate by socket programming and I have to put the capsule between 2 points. Rotate method need and angle x y z value. All I am asking how to calculate that. or some other method all ready exist.

5. ### FatinFardaous

Joined:
Nov 9, 2022
Posts:
6
Brother I am stuck at how to calculate the angle for rotation.

6. ### DevDunk

Joined:
Feb 13, 2020
Posts:
3,470
Explain the issue you have and what you want to do, then I might be able to help

7. ### RichAllen2023

Joined:
Jul 19, 2016
Posts:
977
Your first problem's using Python in Unity, it uses C#!

DevDunk likes this.
8. ### FatinFardaous

Joined:
Nov 9, 2022
Posts:
6
let say 2 point location is given. point a and point b. I need to alligned a cylindrical object or capsule object between 2 point shown in picture.

File size:
2.5 KB
Views:
24
9. ### Olmi

Joined:
Nov 29, 2012
Posts:
1,553
@FatinFardaous first calculate a vector between points A and B, then align your object along that vector. Use existing Unity commands to do that.

Something like this would align an object between 2 points I think:
Code (CSharp):
1. // Calculate a vector from point A to B
2. Vector3 vectorAB = pointB.transform.position - pointA.transform.position;
3. // Calculate the center point
4. Vector3 center = (pointA.transform.position + pointB.transform.position) / 2f;
5. // Set position of the object if your pivot is in the center
6. alignedObject.transform.position = center;
7. // Adjust up vector to rotate the object
8. alignedObject.transform.up = vectorAB;

There are many different ways to do this, I think you could use LookAt, or calculate an angle from the AB vector, and then use Quaternion.Euler to set the rotation if you just need to do a rotation on one plane. But this pseudo code I showed should work for 3D.

Last edited: Jan 24, 2023
10. ### RichAllen2023

Joined:
Jul 19, 2016
Posts:
977
Well done, I might have a go at that meself at some point

FatinFardaous likes this.
11. ### FatinFardaous

Joined:
Nov 9, 2022
Posts:
6
Thanks for the help.