Question by
Kamekasiguy · May 16, 2017 at 08:27 AM ·
noobforces
If i push an object with rigidbody why does it float away even tough i have a force going forward
I have a cube that i've made follow me around and i have a rigidbody on it and a box collider. When i collide with it the cube just floats away. I made the force stronger the further away it got to fix it but when it comes close as it loses the extra speed it goes back because the force pushing it away is stronger. Help please, thanks in advance.
Code for the cube:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AImovement : MonoBehaviour {
public Transform leader;
private float dist;
private Vector3 direction;
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//transform.LookAt(leader);
dist = Vector3.Distance(leader.position, transform.position);
print("distance to leader: " + dist);
if (dist > 3 && dist < 15)
{
direction = leader.position - transform.position;
direction.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 0.9f * Time.deltaTime);
transform.Translate(0, 0, speed);
}
if (dist >= 15)
{
transform.Translate(0, 0, speed * 4);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612122116im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
if with two conditions not working 1 Answer
error the name 'lightDirty' does not exist in current context. 1 Answer
Script not doing anything 1 Answer
Arcade name input with two buttons 0 Answers