I am creating 2D top down shooter game but i am stuck at the movement. Help!
I am creating 2D top down shooter game but i am stuck at the movement where i made my character face the cursor but once i put the movements inside the script the character would only move around the cursor , i only want them to face the cursor and not making the cursor the center point of movement .
using UnityEngine;
using System.Collections;
public class CharControl : MonoBehaviour {
public Vector2 speed = new Vector2(30,30);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 cursorPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
cursorPos.z = 0;
transform.rotation = Quaternion.LookRotation (Vector3.forward, cursorPos - transform.position);
float inputX = Input.GetAxis ("Horizontal");
float inputY = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (speed.x * inputX, speed.y * inputY, 0);
movement *= Time.deltaTime;
transform.Translate (movement);
}
}
thanks in advance!
Answer by Nathuram · Feb 12, 2016 at 09:19 AM
If you just add a RigidBody2D to your character you can do your movement script in a easier way.
What i would do is getting the Input of the Key that moves your character for instance Q for moving up, setting that input key a value of y=1 so S would be -1 and so on.
Then you can do something like that to create the movement :
void FixedUpdate ()
{
GetComponent().velocity = new Vector2(inputX* Speed, inputY * Speed);
}