- Home /
Mesh won't travel in direction it's facing
Hi,
I have an empty object instantiating 'shrapnel' gameobjects at a certain point in space.
This is a 2D game, so I only want objects to move in X and Y, not in Z.
When the pieces of shrapnel are instantiated, the gameObject has this script attached:
using UnityEngine; using System.Collections;
public class shrapnel : MonoBehaviour {
float speed = 10;
Vector3 randomDirection;
// Use this for initialization
void Start () {
float randomAngle = Random.Range (-135, 45);
randomDirection = new Vector3(0, 0, randomAngle);
transform.Rotate(randomDirection);
transform.forward = transform.rotation * new Vector3(1,1,1);
}
// Update is called once per frame
void Update () {
transform.Translate( transform.forward * speed * Time.deltaTime, Space.Self);
}
}
Now the best that I've been able to do is to get the object to rotate around its Z-axis, but then it just keeps travelling 'into' the screen (i.e. away from the orthographic camera) rather than downwards (which is what I'm aiming for).
The above script has my 'bits of shrapnel' firing at all angles, even though I've only asked for them to rotate in one direction.
So, ultimately, what I'm looking to do is when this object spawns it rotates to an angle within a range (pointing down in an angle) and then moves in that direction down the screen.
What have I missed here?
Many thanks in advance.
Try replacing "Space.Self" with "Space.World"
transform.Translate( transform.forward * speed * Time.deltaTime, Space.World);
That definitely helped - now the rocks are moving. But they're still moving in 3 dimensions, when they should only be moving in two. As far as I can see, I've only asked the prefab to rotate around one axis, meaning it should only travel along 2 axes. I've obviously missed something though.
Answer by $$anonymous$$ · Sep 09, 2013 at 04:49 PM
The following code should work.
using UnityEngine;
using System.Collections;
public class shrapnel : MonoBehaviour
{
float speed = 10;
Vector3 randomDirection;
// Use this for initialization
void Start ()
{
float randomAngle = Random.Range (-40, 40);
// Rotate object around z axis
transform.Rotate(0f, 0f, randomAngle);
}
void Update ()
{
// (transform.up * -1) means objects down direction.
transform.Translate( (transform.up * -1) * speed * Time.deltaTime, Space.Self);
}
}
You could also look at AddExplosionForce for an easier way of doing this with Physics.
Awesome, just tried this and it works.
Only surprising thing is that the 'shrapnel' isn't orienting the way I thought it should be, but that doesn't really matter.
If I could trouble you again hdsnevi (because I want to understand what's going on exactly), if I'm telling the object to go down, how come it actually travels in the direction of the rotation?
Is it that down becomes the opposite of where the local 'up' is (seeing as I've specified Space.self)?
Again, thanks regardless.
Please use a comment rather than an answer to reply to existing answers.
Sorry, I thought I'd clicked the comment box. Thanks for pointing that out.