Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Help!

A topic by Odyssey Coder created Sep 19, 2020 Views: 243 Replies: 11
Viewing posts 1 to 7
(1 edit)

I am trying to add rotation to my player and he keeps during wide and I do not know how to fix. Does anyone know. 

My Script:

using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public float MovementSpeed = 1;
    private Rigidbody2D _rigidbody;

    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        var movement = Input.GetAxis("Horizontal");
        transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
        transform.Translate(Input.GetAxis("Horizontal")* 15f * Time.deltaTime, 0f, 0f);
        Vector3 characterScale = transform.localScale;
        if (Input.GetAxis("Horizontal") < 0)
        {
            characterScale.x = -10;

        }
        if (Input.GetAxis("Horizontal") > 0)
        {
            characterScale.x = 10;

        }
        transform.localScale = characterScale;

    }
   

}

Submitted (1 edit)

I would like more of an explanation of what you're trying to do. As far as I understand you want to rotate

If you are using rigidbody anyway you shouldn't be using transform.position instead use _rigidbody.AddForce(new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed) or so.

I don't understand why you change the scale, but I foresee your character getting stuck on something.

Now the actual rotating part. If you just want to rotate your character (lets say 10° every frame)

private void Update()
{
     transform.rotation*=Quaternion.eulerAngles(0,0,10); //EDIT: it should be just Euler instead of eulerAngles!
}

I had my falling out with Quaternions, but now I bested them. Rotation in Unity is represented by them and unlike normal vectors you can't add them you must multiply them.

Alternatively you can use the rigidbody to rotate.

rb.AddTorque(new Vector3(0,0,10*Time.deltaTime*roattionSpeed));

(1 edit)

Alright I put everything in and my code now looks like this (Sorry for being pane in butt I am dummy with barely any C# knowledge)

using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public float MovementSpeed = 1;
    private Rigidbody2D _rigidbody;

    private void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        var movement = Input.GetAxis("Horizontal");
        _rigidbody.AddForce(new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed);
        transform.Translate(Input.GetAxis("Horizontal") * 15f * Time.deltaTime, 0f, 0f);
        if (Input.GetAxis("Horizontal") < 0)
        {
            transform.rotation *= Quaternion.eulerAngles(0, 0, 10);

        }
        if (Input.GetAxis("Horizontal") > 0)
        {
            transform.rotation *= Quaternion.eulerAngles(0, 0, -10);

        }
        

    }

}

I now get the error Quarternion.eulerAngles cannot be used like a method. Error code CS1955

Submitted

Sorry I mixed things up.

Change eulerAngles() to Euler() and the error will go away. I was thinking of a deprecated methode, but it is spelled as EulerAngles. Anyway since it is depricated you should use Euler()!

this would be what it looks like right

 transform.rotation *= Euler(0, 0, -10);

Submitted

No

transform.rotation *= Quaternion.Euler(0,0,-10);

ok thank you sorry for be a dumb arse

Submitted

No problem

It takes some time to get familiar with c# and unity. What programming language have you used before?

A bit of python and a little java

Submitted

Hey, looks like you didn't submit, that's a bummer! I was hoping to see what you were cooking up.

yeah I was not able to get a working prototype my pc also broke next  jam though

Submitted

That's too bad. I would like to wish you much better luck for your next attempt!