How do you make a clone travel at a fixed velocity in the direction of its rotation?
For example if a clone spawns and has values of (0, 0, 0, 1) it travels to the right at a set speed. How would I script that in C# ? Thanks in advance!
I'm not sure what your values (0,0,0,1) should mean. But moving objects can be done on several ways depending on how your object is set up some are better than others. The easiest way to explain here I can think of would be to do:
public float Speed=10f;
void Update(){
transform.position+=transform.forward*Speed*Time.deltaTime;
}
now it moves Forward where Forward is the local Z-Axis of your Object.
Doesn't seem to work. In my script when a clone spawns its direction is either up, right, down or left. What i want to happen is that when a clone spawns it travels in the direction its facing if you understand what I mean?
Answer by OncaLupe · Nov 16, 2015 at 06:33 AM
It depends on what you're going to do with the object and how (if at all) it interacts with other objects. For simple non-physical movement, you can do something like this:
using UnityEngine;
using System.Collections;
public class MoveAlongAxis : MonoBehaviour
{
public float moveSpeed = 5f;
public Vector3 moveDirection = Vector3.forward;
void Update()
{
transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
}
}
Time.deltaTime is how long the current frame took to render. When multiplied to a speed variable, it adjusts the speed to the framerate. So for instance in the above code, moveSpeed is set to 5, so when multiplied to Time.deltaTime it will make the object move at 5 units per second.
moveDirection is the axis the object will move along. Vector3 contains definitions for each of the axis. 'forward' is the Z axis, 'up' for Y, and 'right' for X. When used in the Translate method, it defaults to using local axis. If you need to move by setting the object's position manually, then you'll need to use transform.forward instead of moveDirection to get the local axis.
transform.position += transform.forward * moveSpeed * Time.deltaTime;
If you need physics, then the object will have to have a Rigidbody (or Rigidbody2D) attached. Again for simple movement:
using UnityEngine;
using System.Collections;
public class MoveAlongAxis : MonoBehaviour
{
public float moveSpeed = 5f;
Rigidbody myRigidbody;
void Start()
{
//Store reference to the game object's rigid body. GetComponent is slow.
myRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
myRigidbody.MovePosition(transform.position + (transform.forward * moveSpeed * Time.fixedDeltaTime));
}
}
If using 2D system, replace all Rigidbody with Rigidbody2D.
Issue with this is if it gets blocked, it will constantly try to move, which may or may not work depending on how you want your game to run.
With physics you also have the option to just apply a force when it's spawned and let the physics system handle movement from there. You can either use the Start() method, or on the spawner script to trigger this. This is good for things like bullets or other projectiles.
myRigidbody.AddRelativeForce(moveDirection * moveSpeed, ForceMode.Impulse);
http://docs.unity3d.com/ScriptReference/Transform.Translate.html http://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Hmm, dosen't seem to work as I want it. Is there a way to make it move along its rotation? I tried replacing Vector3 with Quaternion but that gives me a error :/
Your answer
Follow this Question
Related Questions
Scoring system help 1 Answer
Rocket Launcher Fires Explosion, But Not Rocket 2 Answers
Adding a sound to my Gun Script 0 Answers
Button onclick problem. 0 Answers