- Home /
Move objects in a scene based on the main characters local X axis?
Having a hell of time trying to sort this business out. I have a space shooter that lets the ship rotate using transform.rotation = Quaternion.Euler(Vector3(0, angle, 0));
to constantly look at the mouse. The ship itself doesn't move, but the rest of the environment moves around it. At this stage I need to create a strafe, and to the best of my knowledge the only logical way to do that is to have all of the objects which do move in the scene to move on the space ships local x axis. How do I move an Object based on another unrelated objects local x axis? The attempts that I've been working with so far include using a child of the ship itself to create an offset from Vector3(0,0,0) that applies to (1,0,0) at the start of the game. This would likely work in creating my offset, but for some reason when the ship rotates the offset never changes. Essentially when the ship rotates 90 degrees on the Y axis, and now the child object sits at (0,0,1) the input to my code still shows the object (strafeleft) to have a position of (1,0,0). It's killing me. I've tried both transform.position, and transform.localPosition so far no change. Strafeleft is declared as a transform.
if(Input.GetButton("a"))
{
mark = 0;
speed = (movespeed/100) * distance;
movedirection = strafeleft.position;
movedirection.x = transform.position.x + movedirectionb.x;
movedirection.z = transform.position.z + movedirectionb.y;
movedirection.y = -12;
transform.position = Vector3.MoveTowards(transform.position, movedirection, speed*Time.deltaTime);
timerdrift = speed;
speed1 = speed;
}
For all the object's in the scene (assu$$anonymous$$g the character remains level on the XZ plane), you want to add the character's transform.right times some value to move left/right relative to the character. I don't know how you have your game setup, but something like:
transform.position = character.transform.right * speed * Time.deltaTime * direction;
Where direction will be either -1 or 1 depending on which direction you are strafing.
Your answer
Follow this Question
Related Questions
Xbox 360 controller values 1 Answer
Is it possible to create a third Axis? 1 Answer