- Home /
Get an Object to Automatically Move Forward
Hello,
I'm trying to get an object to move forward automatically while being constrained by collisions. Here's my code:
using UnityEngine;
using System.Collections;
public class MoveForward : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
This is the exact same code from the Unity Scripting Reference in C#. You can find that here: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Can someone please tell me what I'm missing. I've been tackling this problem for, literally, weeks. Any input is appreciated. I thank you for your time.
What is you code doing, exactly? Is the thingy moving, but it goes through stuff, or what? The code looks alright at a glance.
Loius, the problem is the object isn't moving. I can move the object when I press the appropriate buttons on my keyboard, but my goal is to get the character moving forward automatically.
Answer by Ghopper21 · Oct 14, 2012 at 05:34 AM
This line:
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))
means you will move only when there is input, due to Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") being used for the x and z values of your movement direction.
To move forward automatically in a simplistic way, use hard-coded x and z values for the direction you want to move in.
If you want to still use input for movement direction but have the character continue to move forward when input has stopped, you can first check if there is input and if not use your existing direction for your movement direction.
This is exactly what I needed. Thank you for your assistance sir.
Answer by Montraydavis · Oct 14, 2012 at 12:24 AM
I may be getting the wrong idea, but, it sounds like something that can be easily done with OnCollisionStay ( ) .
Check if it's colliding with X, then move (x,y,z)
function OnCollisionStay ( collision : Collision) { if ( collision.collider == existingObjectToCheckFor ) {
//do movement code
} }
The problem is the object isn't moving. I can move the object when I press the appropriate buttons on my keyboard but my goal is to get the character moving forward automatically.
Forget collision right now. The object does collide. So right now I'm trying to get the object to move forward automatically. What do you think I need to do?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I move component of one gameobject to other. 3 Answers
moving platforms? 0 Answers