#Need help with 2d rotation in my new game.
1 messages · Page 1 of 1 (latest)
bro is making geometry dash
Yes but only spaceship mechanics for now
In which line should i make change
I tried that but it's rotating in in 90 and -90 only
I would do something more like:
transform.eulerAngles = Vector3.forward * velocity.y * intensity;
It's rotating brother but can't achieve something like the sample i attached
Could you screen record? I dont understand this sentence
Check this out
Well, for me that seems to correlate with the velocity pretty accurately. Your movement is just very far a way from the one in the example, that movement looks a lot more like the zig zag one. If you manage to make the movement of the plane better, it will work nicely, i, pretty confident on that
Actually i want to do something like this
I know but currently the way your plane moves is really far away from that. You should first fix that
I managed to something like this
Still not even close
Can you give me some suggestions regarding that
Well, now that I look your code closer, it doesnt make much sense to me. Theres so many things that do nothing and those lines that actually do, seems like they are meant to do something much different than what they do now. Id just throw all that movement code into trash bin and start again. This is how I would approach this movement: it seems the x movement speed is always constant, no need to touch that. Y velocity on the other hand should increase when player presses a key and decrease when they dont. So you could make two variables: speedX which you only change in inspector and never via script and speedY which you change like this (pseudocode):
if key is pressed then
increase speedY by rate of rotSpeed units per second
else
decrease speedY by rate of rotSpeed units per second
After that you can just set rb.velocity = new Vector2(speedX, speedY); and it should work nicely. If it starts working, you can then consider clamping the speedY to some reasonable range but only after the basic movement works. Btw. loading .txt files to my laptop/phone whatever and opening them is annoying, use either codeblocks or paste sites for longer codes, its much easier for us and they both support syntax highlighting which makes the code much easier to read
Thanks brother for your help
Let me try with your method
Also after that how should I attempt the rotation?
I'm pretty sure the rotation works already with transform.right = rb.velocity, it's just that rb.velocity was jumping up and down in unwanted way
Thanks brother
Thanks mate it worked 🙂
Also brother I am new to unity and this is my first project. Can you give me some suggestions on how should i proceed and learn more?
could you show the code again so I could check if the code is ok?
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class SpaceshipController : MonoBehaviour
{
[Header("Components")]
private Rigidbody2D rb;
[Header("Player Movement")]
[SerializeField] float xSpeed;
float ySpeed;
[SerializeField] float minOffset;
[SerializeField] float maxOffset;
[Header("Player Rotation")]
[SerializeField] float rotSpeed;
[SerializeField] float rotIntensity;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
ySpeed += rotSpeed * Time.deltaTime;
}
else
{
ySpeed -= rotSpeed * Time.deltaTime;
}
rb.velocity = new Vector2(xSpeed, ySpeed);
transform.right = rb.velocity;
transform.position = new Vector2(transform.position.x, Mathf.Clamp(transform.position.y, minOffset, maxOffset));
}
}
[]cb
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class SpaceshipController : MonoBehaviour
{
[Header("Components")]
private Rigidbody2D rb;
[Header("Player Movement")]
[SerializeField] float xSpeed;
float ySpeed;
[SerializeField] float minOffset;
[SerializeField] float maxOffset;
[Header("Player Rotation")]
[SerializeField] float rotSpeed;
[SerializeField] float rotIntensity;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
ySpeed += rotSpeed * Time.deltaTime;
}
else
{
ySpeed -= rotSpeed * Time.deltaTime;
}
rb.velocity = new Vector2(xSpeed, ySpeed);
transform.right = rb.velocity;
transform.position = new Vector2(transform.position.x, Mathf.Clamp(transform.position.y, minOffset, maxOffset));
}
}
seems good now
Thanks Brother
I'd put that in FixedUpdate instead of Update tho
Yeah Sure I'll update that
Can you give me some tips regarding how should i learn more?
I did one course of unity on udemy now i'm proceeding to do my own small games
just add small aspects to the game, watch tutorial video if you don't know how. score system? save highscore? spawn random obstacles (flappy bird like obstacles, you could make some premade set of obstacles that you can spawn at different height)? or maybe just handcraft multiple maps with increasing difficulty? make the game end when you collide with something? make the game get increasingly hard (everything gets faster over time? the longer you survive, the better)? add sound effects? maybe some visual effects?