moshe ralf moshe ralf - 1 month ago 7
C# Question

How can i create a 3d model from a given two points?

I want to draw a line between two points and then rotate or more to convert it to a 3d. Like making a building. If it's a cube or cylinder.

Draw a line i mean not to draw it and display the line or maybe i should draw it and show it but by the logic i mean to draw a line between two given points and then to make it 3d.

I tried this:

using UnityEngine;
using System.Collections;

public class MakeTwoPoints3D : MonoBehaviour {

public Vector3 pA;
public Vector3 pB;

// Use this for initialization
void Start () {

// Assuming this is run on a unit cube.
Vector3 between = pB - pA;
float distance = between.magnitude;
transform.localScale.x = distance;
transform.position = pA + (between / 2.0);
transform.LookAt(pB);
}

// Update is called once per frame
void Update () {

}
}


But getting errors on the line:

transform.localScale.x = distance;



Severity Code Description Project File Line Suppression State
Error CS1612 Cannot modify the return value of 'Transform.localScale' because it is not a variable MakeTwoPoints3D.cs 15 Active


And on the line:

transform.position = pA + (between / 2.0);



Severity Code Description Project File Line Suppression State
Error CS0019 Operator '/' cannot be applied to operands of type 'Vector3' and 'double' MakeTwoPoints3D.cs 16 Active

Answer

transform.localScale.x = distance;

You need to create new Vector3 from distance variable then assign it to transform.localScale.

transform.localScale = new Vector3(distance, transform.localScale.y, transform.localScale.z);

transform.position = pA + (between / 2.0);

Simply add f after the 2.0 to make it float. Without the f, 2.0 becomes double type.

transform.position = pA + (between / 2.0f);

See here. The division overload method takes float not double. Looking at your other Unity question, you need to understand basic Unity Vector arithmetic. See Here and Here.

Comments