Hello there,
It seems that nobody responded to my previous question, it also seems that it was a bit difficult to do then…
But I won’t give up yet!
So, I wanted to ask how to make my main character (Titus) aim at a ‘aim point’ that I control separately.
So basically I want that ‘aim point’ to be controlled with the IJKL keys, but I want to move my Titus with WASD keys, and when I shoot a projectile, it is shot towards my ‘aim point’.
here are my scripts in the following order : Mouse control and shoot and then the movement script of ‘Titus’.
using UnityEngine;
using System.Collections;
public class BulletPrototype1 : MonoBehaviour
{
public float maxSpeed = 25f;
public GameObject Bullet;
private Transform _myTransform;
private Vector2 _lookDirection;
private void Start()
{
if (!Bullet)
{
Debug.LogError("Bullet is not assigned to the script!");
}
_myTransform = transform;
}
private void Update()
{
/*
mousePos - Position of mouse.
screenPos2D - The position of Player on the screen.
_lookDirection - Just the look direction... ;)
*/
// Calculate 2d direction
// The mouse pos
var mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
// Player Camera must have MainCamera tag,
// if you can't do this - make a reference by variable (public Camera Camera), and replace 'Camera.main'.
var screenPos = Camera.main.WorldToScreenPoint(_myTransform.position);
var screenPos2D = new Vector2(screenPos.x, screenPos.y);
// Calculate direction TARGET - POSITION
_lookDirection = mousePos - screenPos2D;
// Normalize the look dir.
_lookDirection.Normalize();
}
private void FixedUpdate()
{
if (Input.GetButtonDown("Fire1"))
{
// Spawn the bullet
var bullet = Instantiate(Bullet, _myTransform.position, _myTransform.rotation) as GameObject;
if (bullet)
{
// Ignore collision
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());
// Get the Rigid.2D reference
var rigid = bullet.GetComponent<Rigidbody2D>();
// Add forect to the rigidbody (As impulse).
rigid.AddForce(_lookDirection * maxSpeed, ForceMode2D.Impulse);
// Destroy bullet after 5 sec.
Destroy(bullet, 5.0f);
}
else
Debug.LogError("Bullet not spawned!");
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerMovement2 : MonoBehaviour {
Rigidbody2D PlayerBody;
Animator Animi;
void Start () {
PlayerBody = GetComponent<Rigidbody2D>();
Animi = GetComponent<Animator>();
}
void Update () {
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
Animi.SetBool("Walking", true);
Animi.SetFloat("Input_x", movement_vector.x);
Animi.SetFloat("Input_y", movement_vector.y);
}
else
{
Animi.SetBool("Walking", false);
}
PlayerBody.MovePosition(PlayerBody.position + movement_vector * Time.deltaTime);
}
}
Well, those are the two scripts.
Can someone give me some help with this?
I do not want a ‘script that is already made for me’ without information about how it works.
I want an explanation about how to do it, instead of having it all done for me.
Even though I am no great programmer I still want to try it myself!
honestly I do not understand how I can make an object that my ‘Titus’ aims to.
the movement of the object can be done simply, and I can change it in control config in unity game-play settings. The hard part is connecting those two, and make them work correctly.
If you want more information about what kind of game I am making,
It is a 2-D top down platform game (not turn based if it is important to know), so there is no gravity, but there are physics like bouncing, walls, doors and mechanics.
Thank you all in advance and have a nice day,
Daniel Nowak Janssen