Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Making a Character rotate.

A topic by RileyT33 created Jan 08, 2021 Views: 203 Replies: 5
Viewing posts 1 to 4

So I don't know if I could ask questions about code, but I am trying to make it so my character could rotate. I tried everything I could think of and still couldn't get it too work. If anyone is reading this, do you know what's wrong? 

The Code:

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

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb2D;
    private float thrust = 10f;
    // Start is called before the first frame update
    void Start()
    {
        transform.position = new Vector3(0f, 0f, 0f);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("w"))
        {
          print("Test");
        }
        if (Input.GetKeyDown("a")){
          transform.rotation = Quaternion.Lerp(0f, 0f, transform.rotation * 10);
        }
    }
}

transform.eulerangles will get you the vector3 you see in the editor

How would I implement it in my code, it keeps saying that I can't add a Vector3 to a float. This is my new code.

using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb2D;
    private float thrust = 10f;
    // Start is called before the first frame update
    void Start()
    {
        transform.position = new Vector3(0f, 0f, 0f);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("w"))
        {
          print("Test");
        }
        if (Input.GetKeyDown("a")){
          transform.eulerAngles = new Vector3 (0f, 0f, +10f);
        }
    }
}

 transform.eulerAngles += new Vector3 (0f, 0f, 10f);

or perhaps  transform.eulerAngles = new Vector3 (0f, 0f,  transform.eulerAngles.z +10f);

if that doesnt work

Okay, so the first solution works, now the only problem is that now it will only do it each time I press A. Is it the if statement, or the code itself?

if statement "down" returns  true only the frame where the player starts the press "up" gets it the moment its released and without either (just getbutton("a") or whatever) will return every frame that button is being held dow