Unknown source is changing an object's Rigidbody velocity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnfieldScript : MonoBehaviour
{
    public int BlowbackForce = 30;
    public GameObject Position;
    private float BlowbackCos;
    private float EndPosition;
    private float ReturnX2;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            StartCoroutine(Fire());
        }
    }
    IEnumerator Fire()
    {
        BlowbackCos = 0f;
        for (int i = 0; i<= 50; i++)
        {
            BlowbackCos = Mathf.Cos(Mathf.PI*i/50);
            Position.gameObject.GetComponent<Rigidbody>().AddForce(0, BlowbackForce*BlowbackCos, -BlowbackForce*BlowbackCos);
            yield return new WaitForSeconds(0.01f);
        }
        Position.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
        Vector3 EndPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        Debug.Log(Position.gameObject.GetComponent<Rigidbody>().velocity);
        for (int i = 50; i > 0 ; i--)
        {
            Debug.Log(Position.gameObject.GetComponent<Rigidbody>().velocity);
            transform.position = new Vector3(Mathf.Lerp(0.54f, EndPosition.x, 1-(i^2)/2500), Mathf.Lerp(0.4f, EndPosition.y, 1 - (i ^ 2) / 2500), Mathf.Lerp(0.54f, EndPosition.z, 1 - (i ^ 2) / 2500));
            Debug.Log(Position.gameObject.GetComponent<Rigidbody>().velocity);
            yield return new WaitForSeconds(0.01f);
        }
        Debug.Log(Position.gameObject.GetComponent<Rigidbody>().velocity);
    }
}

Hello. I’m currently trying to make an animation so that a gun will return to it’s default position after firing it. The script is attached to the gun object and is the child of an empty parent (public GameObject Position). In and a few lines before the second “for” statement, I stop the velocity of the gun’s parent and try to return it to its default position using transform.position and mathf.lerp. Although stopping the velocity of the gun’s parent should set it to zero because there are no scripts afterwards that alter its velocity, it sometimes has a velocity of (0, -2, 2). Using Debug.Log I tried to figure out when the velocity is created and the first three Debug.Logs have a velocity of (0,0,0). This means that the second time the script inside the second “for” statement is called, some unknown force is putting velocity on it. The world I am performing this on has only the collider for a single plane. The gun, which doesn’t have a collider, already has its mysterious velocity before touching the plane. The fact the gun sometimes does and doesn’t have its unwanted velocity confuses me more. Am I making a mistake here? If there’s any more context I can provide you, please ask. Thank you for your time.

Hello,

Have you noticed any common pattern for when this mysterious force in your game is acting on your GameObject Position. Is it for example related to how you press the mouse button, or is it completely random?

You should consider cleaning all that up with a lot more single-line statements and less GetComponent() stuff splattered everywhere… it makes it very hard to even see what you’re trying to do.

Also, NEVER set the transform of a Rigidbody object directly. You are confusticating the physics system.

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

With a wheel collider, use the motor or brake torque to effect motion.

@arfish and @Kurt-Dekker Thank you very much for your responses. I apologise for the late response.

arfish I couldn’t find any common patterns. Sorry.

Kurt-Dekker I implemented .MovePosition(). To do this, I tried to use a method called Transformposition which converts local position to world position. However, it doesn’t seem to work properly. The gun returns to a completely different position. For the past few days, I’ve been trying to work out why and decided to just create a new object to experiment how Transformposition works. I created an empty object and set its xyz positions to 100. I then created another empty object and made it a child of the empty object. This new object has xyz positions of -99. This should mean that the child’s world position should be (1,1,1). To test this, I wrote the following script
Debug.Log(transform.TransformPoint(new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z)));
and attached it to the child object. This however, returns a value of (-98,-98,-98). I’ve tried really hard to find how the Transformpoint method works and possible other people who have had a similar problem to mine but I couldn’t find anything. Would you happen to know why I am not getting a value of (1,1,1)? Thanks

This movie might dispel some fog for you.