unity 2d c# movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movement2D : MonoBehaviour
{
    Rigidbody2D body;

    float horizontal;
    float vertical;

    public float runSpeed = 10.0f;
    
    
    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();        
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);   
    }
}

3.6
5
Peanutjelly 125 points

                                    //This is not a Plataformer type movement 
//Its more of like the old pokemon games type movement if you know what im talking about
//Body Type on RigidBody 2D must be set to kinematic
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D body;

    float horizontal;
    float vertical;

    public float runSpeed = 10.0f;


    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent&lt;Rigidbody2D&gt;();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw(&quot;Horizontal&quot;);
        vertical = Input.GetAxisRaw(&quot;Vertical&quot;);
    }

    private void FixedUpdate()
    {
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    }
}

3.6 (5 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
2d movement script c# unity 2d player movement movement code unity 2d movement in 2d unity simple 2d unity movement unity 2d movement with physics unity 2d basic movement script unity movement 2d code 2d game movement unity 2d movement player unity movement in unity 2d c unity 2d make movement unity c# movement script 2d c# unity 2d movement unity best way to handle 2d movement 2d movement in unity script make 2d movement unity unity c# player movement 2d movement script c# 2d movement script c# 2d basic movement script c# 2d how to add movement in unity 2d unity code for 2d movement how to code 2d movement in unity how to make player movement in unity 2d unity 2d movement tutorial 2d player movement unity c# 2d player movement c# movement 2d unity C# script for player movement 2d in unity 2d movement c# unity script for 2d movement physics based movement unity 2d simple 2d movement script unity c# 2d player movement script simple 2d movement unity 2d movement script unity c# basic 2d movement script unity unity 2d player movement script c# unity 2d game player movement unity 2d c# movement 2d physics based movement unity C# 2d movement unity unity movement 2d movement unity 2d 2d unity movement basic 2d movement unity 2d movement script unity unity movement script 2d 2d movement in unity how to make a 2d movement script in unity unity 2d game movement how to make 2d movement 2d movement unity unity 2d movement code movement script unity 2d how to make a movement script in unity 2d Unity movement 2d script unity 2d movement how to do 2d movement in unity code for movement in unity 2d movement in unity 2d how to make 2d movement in 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