- Home /
How to select one character and play it's animation?
I have multiple character in my project. I wanted to select one among them using mouse click and then move it using arrow key.
I wrote following script but it only selecting player and not playing animations required on key press. Also while one character is selected other has to be at same position only.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camtochar : MonoBehaviour
{
// Use this for initialization
float movementSpeed = 3.0f;
GameObject player;
GameObject player2;
// Use this for initialization
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
player2 = GameObject.FindGameObjectWithTag("Player2");
}
public string Names()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray rayu = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayu, out hit))
{
//StartCoroutine(ScaleMe(hit.transform));
Debug.Log("You selected the " + hit.transform.name);
return hit.transform.name;
}
}
return "";
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
string n = Names();
Debug.Log(n);
if ("Male1" == n)
{
Debug.Log("Inside Male1");
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 2.0f;
if (Input.GetKey(KeyCode.Space))
{
player.GetComponent<Animator>().Play("wave");
player.transform.Translate(0f, 6f * Time.deltaTime, 2.0f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftShift))
{
// ani.Play("run");
player.transform.Translate(0f, 0f, 6f * Time.deltaTime);
}
if (Input.anyKey)
{
Debug.Log("Inside Male1.anyKey");
player.GetComponent<Animator>().Play("walk");
player.transform.Rotate(0, x, 0);
player.transform.Translate(0, 0, z);
}
else
{
player.GetComponent<Animator>().Play("wave");
}
}
else if ("Male2" == n)
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 2.0f;
if (Input.GetKey(KeyCode.Space))
{
player2.GetComponent<Animator>().Play("wave");
player2.transform.Translate(0f, 6f * Time.deltaTime, 2.0f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftShift))
{
// ani.Play("run");
player2.transform.Translate(0f, 0f, 6f * Time.deltaTime);
}
if (Input.anyKey)
{
player2.GetComponent<Animator>().Play("walk");
player2.transform.Rotate(0, x, 0);
player2.transform.Translate(0, 0, z);
}
else
{
player2.GetComponent<Animator>().Play("wave");
}
}
}
else
{
player2.GetComponent<Animator>().Play("wave");
player.GetComponent<Animator>().Play("wave");
}
}
}
Also I want to move my camera to selected object (about which I don't have any idea how to move it)
Suggest something!!
Answer by text23d · Nov 30, 2017 at 06:57 PM
hm, why do you use same code for Male2 and Male1? just make public void GetGameObjectOnClick() {} function, and let Raycast hit return object clicked : return hit.transform.gameobject; then you can make same code for both Males.
there are many projects on assets store with keyboard input controllers. also check assets which come with unity3d installs. there used to be man-with-wrench gameobject with a script for movement on keyboard.
and as for camera. make separate script for camera, and make there 3 variables - offsets for x,y,z which are to be added. set camera's target to object which you get from return hit.transform.gameobject;
maybe this helps a bit