top down movement unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
5
2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
// This is if you have a float in an animator you want to change if moving
animator.SetFloat("Speed", movement.sqrMagnitude);
// Flip player when x change, use only if you want
Vector3 characterScale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
characterScale.x = -1;
}
if (Input.GetAxis("Horizontal") > 0)
{
characterScale.x = 1;
}
transform.localScale = characterScale;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
Thank you!
2
0
4
8
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
public float speed;
private Vector2 targetPosition;
void Start()
{
targetPosition = new Vector2(0.0f, 0.0f);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPosition = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(targetPosition.x, targetPosition.y, 0.0f));
}
this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
}
}
Thank you!
8
0
4
2
Rigidbody2D body;
float horizontal;
float vertical;
float moveLimiter = 0.7f;
public float runSpeed = 20.0f;
void Start ()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
Thank you!
2
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity