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
Awgiedawgie 440215 points

                                    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);
    }

    
}

5 (2 Votes)
0
4
8
Awgiedawgie 440215 points

                                    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);
           
       
    }
}

4 (8 Votes)
0
4
2
Krish 100200 points

                                    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);
}

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
unity topdown movement how to code top down movement unity unity topdown movement player movementto down unity top down player movement unity ship top down movement unity top down unity movement unity 3d top down movement unity top down movement script unity script movement up and down how to make top down movement in unity top down 3d movement script unity topdown movement script unity 3d topdown movement script unity unity top down rpg movement unity how to make a top down game unity topdown movement code unity top down mouse movement unity topdown movement script basic top down movement script unity unity 3d top down movement script unity top down movement 3d unity topdown player movement running in top down movement unity top down movement unity7 top down movement unity 3d unity 2d movement top down script unity 2d top down movement versus point unity 2d top down player top down movement unity mouse unity top down player movement wasd movement unity topdown 2d unity top player top down player controller unity rb unity 2d topdown character controller Unity 2D Topdown movement top down games movement top down player movement top down character movement unity unity movement top down 2d character top down movement unity script unity 2d up down movement 2d top down movement physics olayer top down movement 2d top down movement unity topdown movement unity 2d top down movement script unity topdown rpg movement unity 2d up and down movement unity 2d top down movement topdown controller top down velocity top down rigidbody movement top down player controller smooth movement unity top down player controller how to implement rpg 2d movement in unity TOP DOWN MOVEMENT in Unity controller movement unity top down top down movement uyntiy 2d basic 2d top down movement in unity top down 2d movement unity how to make a top down character in unity2d unity 2d rpg movment unity 2d rpg movement simple top down movement top down movement unity script top down player controller unity create 2d top down movement unity 2d movement top down for Unity 2018 top down movement script unity unity 2d character controller to pdown 2d rpg movement unity unity top down 2d character controller unity 2d movement top down 2d player unity top down top down unity 2d movement top down 2d movement script simple top down movement unity up and down 2d game top down character controller unity unity 2d top down character controller rigidbody2d topdown unity how to make a top down player unity top down character controler unity 2d movement controller script top down -site:pinterest.* how do i do top down movement unity top down simple movement unity 2d topdown movement unity how to make top down character move in unity 2d 2020 2d movement unity up down unity top down 2d movement unity 2d top down player movement script top down unity movement script top-down 2d unity script unity top down rigid body 2ds top down movement unity brackeys perfect top down movement unity unity 2d top down player movement top down movement c# unity 2d top dowon player controller unity make good topdown movement proper 2d rpg movement c# c# player movement rpg unity best movement method for top down shooter unity 2d player controller top down how to go up on a topdown 2D game unity c# top down game movement unity 3d unity 2d rpg movement script unity simple top down movement unity top down character controller how to make a top down movement in unity 2020 how to make a top down in unity 2020 unity 2d top down movmen unity top down movement unity best movement in top down 2d game unity physics based top down controller top down movement unity 2d top down movement unity
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source