0
\$\begingroup\$

Background: I am playing around with a simple isomorphic 3D shooter where a player controls a character which can shoot other players. Here's an example scene where you can also see the hierarchy for the game objects and the controlling script:

enter image description here

As you can see, I have created a simple turret which consists of three parts:

  • Guns
  • Torso
  • Bottom

Just to be more clear, here's a screenshot of the turret close up with its armature:

enter image description here

In Unity, I have already created a simple PlayerController.cs script (see beloc) which allows a player to move the entire game object around using W, A, S, D and look using the mouse.

However, I want to rotate the Bottom independently. So if the Player moves left, the bottom should rotate towards that direction but the torso should still focus on the mouse position.

Now the first question is how I can do that. I don't know how to get access to the game objects and rotate them independently. The second question is how I can or should design the player game object s.t. I can exchange models more easily. What I mean by that is: Imagine the player switches the weapon. Obviously, only the guns should change but e.g. not the entire character. Only the mesh etc. should change dynamically.

I am sure this isn't that hard to accomplish but I couldn't find a simple example that kind of explains how to do it.


PlayerController.cs

public class PlayerController : MonoBehaviour
{

    [SerializeField]
    public float moveSpeed = 1f;
    private Vector3 _forward;
    private Vector3 _right;
    public LayerMask layerMask;

    // Start is called before the first frame update
    void Start()
    {
        this._forward = Camera.main.transform.forward;
        this._forward.y = 0;
        this._forward = Vector3.Normalize(this._forward);
        this._right = Quaternion.Euler(new Vector3(0, 90, 0)) * this._forward;
    }

    // Update is called once per frame
    void Update()
    {        
        if (Input.anyKey) 
        {
            this.Move();
        }

        this.MouseLook(); 
    }

    public void Move()
    {
        var rightMovement = this._right * Input.GetAxis("HorizontalKey");
        var upMovement = this._forward * Input.GetAxis("VerticalKey");
        transform.position += (Time.deltaTime * this.moveSpeed * Vector3.Normalize(rightMovement + upMovement));

        var heading = Vector3.Normalize(rightMovement + upMovement);
        if (Vector3.Magnitude(heading) > 0.0)
        {
            transform.forward = heading;
        }
    }

    void MouseLook()
    {
        Ray castPoint = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(castPoint, out hit, float.MaxValue, layerMask))
        {
            Vector3 direction = hit.point - transform.position;
            direction.y = 0.0f;
            transform.forward = Vector3.Normalize(direction);
        }
    }
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

A few ways you can achieve that depending on how you want to set it up.

Parent //the parent with the script on it.

//the children in the parent are numerically ordered starting at 0

Guns =0

Torso =1

Bottom=2

For the guns you always have it look in the direction of the mouse position transform.LookAt();

For the bottom you can either rotate the object in code behind or just make a few simple move animations and set to run as you move around.

To get access to those children objects you can use transform.GetChild()

for example

var Guns = transform.GetChild(0);
var Torso= transform.GetChild(1);
var Bottom = transform.GetChild(2);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .