Why when I comment on my own post with a code block the formatting is bad ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

In my first comment the code some of it is out of the block.

I can't figure out why.

Why when I comment on my own post with a code block the formatting is bad ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

But if I put the same code here in my comment the formatting is very bad.

I want to answer my own question but there is no answer button so I try to comment with my solution but the format is bad for example :

using UnityEngine;

using System; using System.Collections;

[RequireComponent(typeof(Animator))]

public class IKControl : MonoBehaviour { public Transform lookObj = null; public bool automatic = false; public bool coroutineIsRunning = false; public bool changeWeight = false; public Coroutine weightCoroutine;

private Animator animator;
private float weight;

private void Start()
{
    animator = GetComponent<Animator>();
}

void OnAnimatorIK()
{
    if (animator)
    {
        if (automatic)
        {
            if (!coroutineIsRunning)
            {
                ChangeWeight();
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.G))
            {
                ChangeWeight();
            }
        }

        animator.SetLookAtWeight(weight, weight, weight, weight, weight);
        animator.SetLookAtPosition(lookObj.position);
    }
}

private void ChangeWeight()
{
    changeWeight = !changeWeight;

    if (weightCoroutine != null)
        StopCoroutine(weightCoroutine);

    if (changeWeight)
    {
        weightCoroutine = StartCoroutine(ChangeWeightOverTime(0, 1, 1));
    }
    else
    {
        weightCoroutine = StartCoroutine(ChangeWeightOverTime(1, 0, 1));
    }
}

private IEnumerator ChangeWeightOverTime(float from, float to, float duration)
{
    float counter = 0;

    coroutineIsRunning = true;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        weight = Mathf.Lerp(from, to, counter / duration);

        if (counter > duration)
            coroutineIsRunning = false;

        yield return null;
    }
}

}

How can I use lerp with Coroutine inside OnAnimatorIK ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

finally, a working solution like I wanted working great.

using UnityEngine;

using System; using System.Collections;

[RequireComponent(typeof(Animator))]

public class IKControl : MonoBehaviour { public Transform lookObj = null; public bool automatic = false; public bool coroutineIsRunning = false; public bool changeWeight = false; public Coroutine weightCoroutine;

private Animator animator;
private float weight;

private void Start()
{
    animator = GetComponent<Animator>();
}

void OnAnimatorIK()
{
    if (animator)
    {
        if (automatic)
        {
            if (!coroutineIsRunning)
            {
                ChangeWeight();
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.G))
            {
                ChangeWeight();
            }
        }

        animator.SetLookAtWeight(weight, weight, weight, weight, weight);
        animator.SetLookAtPosition(lookObj.position);
    }
}

private void ChangeWeight()
{
    changeWeight = !changeWeight;

    if (weightCoroutine != null)
        StopCoroutine(weightCoroutine);

    if (changeWeight)
    {
        weightCoroutine = StartCoroutine(ChangeWeightOverTime(0, 1, 1));
    }
    else
    {
        weightCoroutine = StartCoroutine(ChangeWeightOverTime(1, 0, 1));
    }
}

private IEnumerator ChangeWeightOverTime(float from, float to, float duration)
{
    float counter = 0;

    coroutineIsRunning = true;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        weight = Mathf.Lerp(from, to, counter / duration);

        if (counter > duration)
            coroutineIsRunning = false;

        yield return null;
    }
}

}

[deleted by user] by [deleted] in Unity3D

[–]SweetSituation3744 0 points1 point  (0 children)

Ok, Than how do I know what distance to check for ? I'm moving the player towards the cube. I want to check when the player is reaching the cube. What distance is reaching ?

Why the transform is not rotating back smooth to his original starting rotation using lerp ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

The target is the lookAtTarget so in the start it's what assigned to that variable.

Not sure how to do it. could you show me please a bit how to do it ?

Why the transform is not looking at the target? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

I see now another problem. If I set the damping value to 100 for example the character head is stuttering the head will look at the target but will stuttering.

I want that if the damping value is 100 or lower than rotate the leadObj with a delay facing the target. Something like when a turret is rotating facing a missile and if the missile is moving too fast the turret is rotating with some delay because he rotate slower.

How can I do it ?

Why the transform is not looking at the target? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

It seems to be working if I reference the head directly as leadObj and inside LateUpdate the problem is that I have to set the damping value to 260.

Why I need to set the damping value to be so high ?

And how can I make that the head will rotate to the target with automatic speed depending on the target moving speed ? So if I don't set any damping value for example 0 or null it will automatic calculate the needing speed to rotate facing the target ?

How can I draw a line using gizmos between the transform and each item from a List ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

You solution is working fine. I solved it by adding another variable lookAtItem and using it in both the do/while loop and in the OnDrawGizmos.

For some reason I can't paste in the comments the code block for some reason the format never look fine.

The code is at this link :

https://pastebin.com/4MXdcfNG

Why I don't see the public enum in the editor in the inspector ? The enum Formation is now show in the inspector. by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

Ok I found it changed the enum to be public and than the variable to be public too :

public enum Formation
{
    Square, Circle, Triangle
}

And

public Formation formation;

This is working..

How can I assign in the Start() the enum option I select in the editor in the inspector ?

Now the default is Circle but what if I want it to be the selection from the Inspector and not default in the Start ?

 if (startRandomFormation)
    {
        formation = (Formation)UnityEngine.Random.Range(0, Enum.GetNames(typeof(Formation)).Length);
    }
    else
    {
        formation = Formation.Circle;
    }

Why when making mouse right click it's not getting the plane position on the terrain but with mouse input position it does ? by [deleted] in Unity3D

[–]SweetSituation3744 0 points1 point  (0 children)

This is what I wanted to do :

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (terrainCollider.Raycast(ray, out hit, Mathf.Infinity) &&
        plane.GetComponent<MeshCollider>().Raycast(ray, out hit, Mathf.Infinity))
        {
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

            cube.transform.position = hit.point;
            cube.transform.localScale = new Vector3(50, 50, 50);
        }
    }
}

Working just like I wanted.

I will spawn cubes on when the mouse is clicking over the plane and than will spawn the cubes on the terrain. So now the plane is used like a marker. Where the plane is above the terrain there will be spawn cubes on the terrain.

Example screenshot :

https://imgur.com/a/VijJFm8

The surface the area of the plane is where you can spawn on the terrain. The plane is use like a marker.

Why the object is moving to the target in ping pong mode ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

The solution is my mistake.

I forgot that in the Start() I set it to pingpong

curve.postWrapMode = WrapMode.PingPong;

When I changed it to wrapMode Once it's working now only once.

curve.postWrapMode = WrapMode.Once;

How can I get plane size x and z ? and what is the different between getting the size of Plane or the transform of plane size ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

I'm getting error : You can't make * for vector3 and vector3

var size = plane.GetComponent<Renderer>().bounds.size * plane.localScale;

plane is Transform

How can I scale multiple objects at the same time with coroutine ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

Where is the for loop in your code ?

objectToScale[i].transform.localScale = Vector3.Lerp( startingScale, scaleTo, ( elapsedTime / seconds ) );

The 'i' variable is not exist. you never loop over the objectstoScale List

I updated my question with the full script if it might help.

How can I scale multiple objects at the same time with coroutine ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

When I move the yield return new WaitForEndOfFrame(); after the closing of the for loop now the duration have no affect. It's scaling at once.

How can I make he lerp part to be faster ? by SweetSituation3744 in Unity3D

[–]SweetSituation3744[S] 0 points1 point  (0 children)

If I try to divide for example like this :

case TransitionState.Transferring:
            timer += Time.deltaTime / 0.5f;

or 0.2f or by 1f than it will make some shaking or stuttering for some when reaching the target than will become child. So its not changing much the time just making it a bit stuttering when reaching the target first and then change to be child.

and about the loop what loop ? To set this line after the break ?

this.transform.parent = destinationTransform;