Hello, fellow scripters!
I’ve been having an issue lately of my character not rotating around the anchor point but rotating around the cursor. Because of this it is hard to navigate the character in the game and I need to resolve this by tomorrow. If anyone can be as kind as to help me please do!
What mine looks like: Screen capture - b89d01625876f852436754e1d5d8f767 - Gyazo
What it should look like: Screen capture - 40e89d62d92d6f8350024be18ed6cc34 - Gyazo
My movement code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
[SerializeField] //So that we don't interfere with any other scripts
private float speed; //Speed variable
private Vector2 direction; //Direction variable
//-------------------------------------------------------------------------------------------------------------------------
void Update () //Every refresh, Check for Move and GetInput
{
Move();
GetInput();
}
//-------------------------------------------------------------------------------------------------------------------------
public void Move() //Declaring what Move is, In this case it is the direction and speed and time it should take to reach the area.
{
transform.Translate(direction*speed*Time.deltaTime);
}
//-------------------------------------------------------------------------------------------------------------------------
public void GetInput() // If they hold down a key, It should return with a movement. [WASD]
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
else
// If they hold down the UpArrow or the key "W" They will move forward or up
if (Input.GetKey(KeyCode.UpArrow))
{
direction += Vector2.up;
}
//-------------------------------------------------------------------------------------------------------------------------
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
else
// If they hold down the LeftArrow or the key "A" They will move to the left
if (Input.GetKey(KeyCode.LeftArrow))
{
direction += Vector2.left;
}
//-------------------------------------------------------------------------------------------------------------------------
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
else
// If they hold down the DownArrow or the key "S" They will move down
if (Input.GetKey(KeyCode.DownArrow))
{
direction += Vector2.down;
}
//-------------------------------------------------------------------------------------------------------------------------
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
else
// If they hold down the RightArrow or they key "D" They will move to the rigt
if (Input.GetKey(KeyCode.RightArrow))
{
direction += Vector2.right;
}
//-------------------------------------------------------------------------------------------------------------------------
}
}
My rotate code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lookat : MonoBehaviour {
void Update ()
{
var mouse = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
Thanks for your time and possible help!