So I have a player. It has hands attached made from two rectangles.(I attached .GIF)
So I need it to move how the player moves right stick on joystick. I made some controls, but I think they are poor, and inaccurate. I believe, it's possible to make it better. Hope I will find some help here. What I made up is an object (invisible, visible only for describing problem, debuging purposes) that moves on axis of joystick. Then there are player, he has hands. On the lower hand lower there is script attached. Before I had made spring, but I didn't liked result. Script:
public GameObject Target;
void Update () {
if (Input.GetKey (KeyCode.J)) {
transform.position = Vector3.MoveTowards(transform.position, Target.transform.position, 1);
}
So the question is, how to upgrade this system hands control system? I would like it to be more physical, because I'm making a volleyball game.
Related
Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)
I'm making a very simple 3D game in Unity where I have this space shuttle I can move around in space around asteroids and I can shoot when pressing/holding the mouse button. I'm doing this by Instantiating a sphere at the "Emitter" transform.position and then just applying a forward Force to that bullet object.
It all works fine, but the one thing I don't like and also don't know how to fix is how the bullets keep their position when shooting and moving the mouse left-right, instead of keeping a perfectly straight line at all times.
This is how it looks when I'm shooting and moving my camera at the same time:
Screenshot while shooting
Here's a gif for better visualization.
Right now it looks like I'm pissing lasers, which is never good. I tried making the bullet speed a lot faster, but then the bullets become harder and harder to see and it doesn't look as good.
This is the code by which I'm shooting the bullets:
private void Fire()
{
GameObject bullet = Instantiate(laserPrefab);
GameObject bullet2 = Instantiate(laserPrefab);
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), shuttleCollider.GetComponent<Collider>());
Physics.IgnoreCollision(bullet2.GetComponent<Collider>(), shuttleCollider.GetComponent<Collider>());
bullet.transform.position = laserEmitter.position;
bullet2.transform.position = laserEmitter2.position;
/*Vector3 rotation = bullet.transform.rotation.eulerAngles;
Vector3 rotation2 = bullet2.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet2.transform.rotation = Quaternion.Euler(rotation2.x, transform.eulerAngles.y, rotation2.z);*/
bullet.GetComponent<Rigidbody>().AddForce(laserEmitter.forward * laserSpeed, ForceMode.Impulse);
bullet2.GetComponent<Rigidbody>().AddForce(laserEmitter.forward * laserSpeed, ForceMode.Impulse);
StartCoroutine(DestroyBulletAfterTime(bullet, bulletDeathTime, bullet2));
}
Don't mind the commented lines, I was just messing around trying to see if I can get it to work. The shooting behaves the same with or without those commented lines.
Of course, a projectile based system will behave and look like a projectile system.
If you want laser behavior use a LineRenderer. Raycast where your laser line should end (either laser max distance or the point of hitting an object in range).
If you don't like the "static" looks of it, change the LineRenderer Material to something that changes over time (search for shaders/ LineRenderer effects).
I am using Unity3D to make a replica of the game "Rolling Sky" which you can find on the Google/Apple app store. I was able to make a simple floor which the ball will move on and I was also able to make the ball (Player) move left and right. After moving the ball a couple of times back and fourth, it starts float in the air and eventually what it seems like is change it's axis.
I came up with a couple of alternatives that I think would work, but I had trouble coming up with the code to make it function properly.
1) Do not rotate the ball at all. Just have it smoothly move back and forth.
^^ Probably the best solution I could come up with.
or
2) Use CharacterController to have complete control of how the ball reacts to different events, scenarios, etc.
^^ This would probably be the most necessary and hardest of I had to guess.
or
3) Move the ball an x amount of pixels everytime I move left or right.
^^ I would assume this would create a lot more glitches than what I have right now.
public class PlayerController : MonoBehaviour
{
public float speed;
void Update()
{
transform.Translate(Vector3.right * speed * Input.GetAxis("Horizontal") * Time.deltaTime);
}
}
EDIT:
I was able to provide my own answer, but if anyone has any alternatives, feel free to post your solution!
To prevent the player ball from floating into the sky, no other code was needed. Simply go to the Rigidbody component on the Player and expand the "Constraints" menu. Next, check the axis boxes that you would like to freeze the rotation on to prevent the ball from spinning like so in the picture.
Click the link to see the picture
here.
In this case, I did not freeze the ball on the x axis because I eventually want to animate it to spin as if it is rolling forward.
You're using transform.Translate
and
You're using Vector3.right
transform.Translate will directly move the ball without thought of rotation. You will need to take the object's rotation into consideration when translating. You can also use the physics engine and use Rigidbody.MovePosition to force position or Rigidbody.AddForce to utilize physics interactions.
Vector3.right will use the constant value equivalent to Vector3(1, 0, 0). This will not be in regard to the object's rotation or facing.
I suggest looking through Unity's tutorial for a ball-roller and see how they complete the task of user input with a user controlled ball.
I'm new to unity and I'm currently working on a portal-like game.
I did the whole teleportation script and it works, but the problem comes that I didn't implement the player camera correction and actually I don't have any ideas how to do it. The concept is that when you're jumping through a portal, the player (or player camera) rotation should be changed to the portal/portal camera rotation from you've come so the final effect is more 'realistic'.
I've tried some lines in teleportation script like player.transform.rotation = portal.transform.rotation but in the end it didn't work and now I end up with nothing, deleting previous scripts and trying to write it all over and over again.
I'll be glad if someone could guide me how to start coding it. Should I do it in onTriggerEnter (when you're jump through portal), or in onTriggerExit? Should the script be attached to a player or to a portals? Should I gather rotation only from camera or from the whole gameobject (portal/player)? I'm posting also couple of screens (with a video how it currently works, and also an entire teleportation script. If I missed something just ask me and I'll post it here.
https://imgur.com/a/pbqYnLD - screens with portals inspector
https://streamable.com/b14hk - video how it works
teleportation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleportation : MonoBehaviour {
[SerializeField] private GameObject otherPortal;
[SerializeField] private GameObject player;
void OnTriggerEnter(Collider col) {
if(col.tag == "Player") {
col.transform.position = new Vector3(otherPortal.transform.position.x+1, otherPortal.transform.position.y+1, otherPortal.transform.position.z+1);
Debug.Log("wszedłem w portal");
}
}
void Update() {
}
}
some informations how it is coded right now:
portals are currently in game behind 'the box', i didnt instantiate them anywhere; just changing position on lpm (blue portal) and ppm (orange portal)
portals are sticking to the walls, just like in the original game
portals have a camera attached to it and right now the cameras are static. (offtop: i have a script to move them exactly when player is moving and it quite works but also have some problems, like camera can get too far away from portal and start rendering only that green outer side of the box, and i also dont know how to fix it, so currently i didnt use this script)
the player movement im using is that from unity standard assets (if it somehow matters)
the player have a rigidbody but the portals dont; not sure if i should attach this component to them
teleportation script is attached to the both of portals - the 'otherPortal' variable is moved from inspector, like in orange portal the 'otherPortal' variable is blue portal and the other way
What you did is correct (setting the player rotation to the portal.
You can do it in onTriggerEnter after setting the position, then it should look like
player.transform.rotation = otherPortal.transform.rotation
If you do that, the player will have the same rotation. You already have something that make the camera follow the player, so it is likely that you don't need to set the camera rotation. I don't know how you did your camera follow, so I can't be sure, though. If the camera has not the proper orientation, doing Camera.main.transform.rotation = otherPortal.transform.rotation will do it.
The remaining thing that might be wring, could be that your player (and camera) is not facing the right axis. On your video, I can see that the portal faces the x-axis (the red axis in Unity editor). Check that when going forward, your player has the red axis looking forward.
It is likely that your player has the z-axis (blue) facing forward, which is (by convention) more correct and fits the names Unity uses (z-axis is also called forward-axis)
I would recomand to create the portal object (and all other objects, including the player) so that the forward-axis is the blue one. It might require editing the objects. Anycase, check that the player forward axis is that same as the portal, otherwise setting the rotation won't work
Okay, so I've got some code to make my enemies follow the player in my XNA game, but they only follow the player until the player is in front of them. If the player moves past an enemy, it will stop moving towards him. Instead they will continuously move up and down with the player.
The code I've used is this:
Vector2 direction = player.Position - goblins[i].Position;
direction.Normalize();
Vector2 velocity = direction * goblins[i].enemyMoveSpeed;
goblins[i].Position += velocity;
(ignore the goblins bit, I've just replaced the graphics)
Not entirely sure where to go with it, any ideas?
Tom, hello, how are you?
Here you have 2 examples that helped me a lot:
Chase & Evade (http://xbox.create.msdn.com/en-US/education/catalog/sample/chase_evade) This is a Microsoft sample that shows how to implement several simple behaviors for AI, including chasing, evading, and wandering.
Adding a field of view to enemies (http://robotfootgames.com/xna-tutorials/5-xna-platformer-starter-kit-field-of-view-for-enemies) Related to number 1 sample and to Plaformer starter kit