- Home /
Colliding With Objects While Automatically Running Forward
I attached a script to a first person controller called Run_Forward so that the user automatically runs forward without pressing a button. However when I run into objects I go right through them or the objects are pushed out of the way. When I freeze every position and rotation on the object I just run through the object. Please help. My code is below.
using UnityEngine; using System.Collections;
public class Run_Forward : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
transform.position += Vector3.right*4;
if (Input.GetKeyDown(KeyCode.D))
transform.position += Vector3.left*4;
transform.Translate(0, 0, .2f);
}
}
Answer by Piflik · Jun 06, 2012 at 10:35 PM
transform.position ignores collision. You'd have to use rigidbodies, if you want collisions.
latsushi : I already have rigid bodies on my objects.
I'll try separating movement and direction with two objects in the hierarchy.
You have RigidBodies, but you don't use them for movement.
($$anonymous$$eywords: AddForce, Velocity)
Your answer
![](https://koobas.hobune.stream/wayback/20220613072444im_/https://answers.unity.com/themes/thub/images/avi.jpg)