Trying to move a player object to the start location built into each level of the game at level start but the object does not reposition. The player is not a child of another so I am working with root transform here.
//move player to start position (I know find is expensive)
startPosition_GO = GameObject.Find("StartPosition");
playerGO.transform.position = startPosition_GO.transform.position;
//and just to be because
playerGO.transform.position = new Vector3(startPosition_GO.transform.position.x, startPosition_GO.transform.position.y, startPosition_GO.transform.position.z);
player object stays where it was and does not move.
including two images, first before starting the level you can see the starting location game object (the pink square). The code above is called at level start. The second image shows where the player (with the arrow nav icon on it sits after the code is executed.
Try just this code to move the player position.
startPosition_GO = GameObject.Find("StartPosition");
playerGO.transform.position = startPosition_GO.transform.position;
Set the position once. You could remove this
playerGO.transform.position = new Vector3(startPosition_GO.transform.position.x, startPosition_GO.transform.position.y, startPosition_GO.transform.position.z);
Or this and it should work.
playerGO.transform.position = startPosition_GO.transform.position;
The code seems redundant which could lead to issues.
Related
Currently I am working on a game for people with accessibility restrictions. I am having the issue of locking the player model in a sitting position. If the user does not center themselves in the room the player model will be pulled to a certain direction. I would like to lock the player model in a seat and only allow for arm movements and head rotations, no leaning or moving in the game using the HMD.
Since I am using the Final VR IK asset I have tried using their demo for sitting position in VR and cannot get the player to stay seated naturally. I am not sure how to program this or set restrictions to be able to do this.
Edit: To simplify my question. How do you lock the oculus rift HMD to only allow for rotation and not position tracking.
I figured it out how to lock the HMD into to only allow for rotation and not position tracking. To add a sitting position just use an body animation that's sitting. There are 2 things I did. First I added a line of code to the OVRCameraRig script:
trackingSpace.localPosition = -1 * centerEyeAnchor.localPosition;
This was done right before the RaiseUpdatedAnchorsEvent(); call around line 260 in the UpdateAnchors() method. What this does is it locks the head position and only allows for head rotation.
The second thing I did was write a head relocation script based on #derHugo answer to one of my other questions. What this does is when the space bar is pressed it will move the entire OVRCameraRig position. There must be a parent on OVRCameraRig for this to work In the screen shot below you can see the CameraParent object as the parent. I used a sphere as the relocation object that was placed in the middle of the head of my player model. The reason I had to add this was sometimes when you hit play the player would start at a weird position depending on where the headset was at the beginning. In the screen shot you can see use position tracking as not being checked in the inspector, that is an error. Please keep it selected to prevent screen tearing in the headset
Here is the code for the changing position of player when in game:
public class VRPositionChange : MonoBehaviour
{
public Transform resetPos;
private Transform parent;
private void Awake()
{
// create a new object and make it parent of this object
parent = gameObject.GetComponentInParent<Transform>();
}
// Update is called once per frame
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
// reset parent objects position
parent.position = resetPos.position;
}
}
}
I have an object in my scene's sky and want to prevent it from getting cut out of the view, no matter the screen ratio. So I set up a canvas and a PlaceHolder RectTransform on it, that - as being UI element - gets scaled and repositioned in respect to the screen aspect ratio. My goal is to use this position to determine where to put my scene object by using the following script:
Camera mainCam;
RectTransform placeHolder;
private void Start()
{
mainCam = GameObject.FindGameObjectWithTag("MainCamera")
.GetComponent<Camera>();
placeHolder = GameObject.FindGameObjectWithTag("SkyObjectPlaceholder")
.GetComponent<RectTransform>();
var screenPos = mainCam.ScreenToWorldPoint(
new Vector3(placeHolder.rect.x, placeHolder.rect.y, 0));
transform.position = new Vector3(screenPos.x, screenPos.y,
transform.position.z);
}
The issue is that my sky object is always placed in the center of the screen. It is not a child of anything on the scene, I just put it there. The placeholder is to the upper right in the canvas. Note that I tried playing with anchors for the UI element, set it to be left bottom, right top, no change, the scene object is always in the center of the screen.
What am I doing wrong? How to make a Scene element match up the position of a Canvas element, so it gets right behind it?
Note: I'm using a perspective camera. I don't know if it matters, but I thought I mention it, just in case.
Yet another edit: after messing around with the issue a bit, I found that the problem is possibly linked to the Z input I give the ScreenToWorldPoint, in my case it's 0. I couldn't get a viable Z to input though.
Hmmm... i think you should change the sorting layer of that object
I have the following setup right now:
"Startup" scene which initializes all managers (spatial mapping, cursors, camera, ...)
"Main" scene with animations and one object which moves around
Once the users interacts with the program the scene restarts at some point, then the following should happen:
Object's coordinates get stored (E.g. DontDestroyOnLoad() on a "storage object". I need to destroy the object since it needs to reset itself on scene restart)
Scene restarts
Object gets recreated with stored coordinates
Code:
DataPositionStorer positionStorer = new GameObject("DataPositionStorer").AddComponent<DataPositionStorer>();
DontDestroyOnLoad(positionStorer.gameObject);
// dataPoints is the specific object I need to keep the coordinates of
positionStorer.Position = dataPoints.transform.position;
positionStorer.Rotation = dataPoints.transform.rotation;
positionStorer.Zoom = dataPoints.transform.localScale;
// Reload current scene ("Main")
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
And in the startup method of the object:
DataPositionStorer positionStorer = FindObjectOfType<DataPositionStorer>();
if (positionStorer!= null)
{
dataPoints.transform.position = positionStorer.Position;
dataPoints.transform.localRotation = positionStorer.Rotation;
dataPoints.transform.localScale = positionStorer.Zoom;
}
This setup works in the unity player but once I run it in the hololens emulator / on a hololens the position afterwards isn't correct anymore.
Can somebody help me resolve this problem on the emulator / hololens?
Thanks to the two guys commenting I've found a solution:
Load the WorldAnchorManager Script in the Setup scene and make it DontDestroyOnLoad
Attach a World Anchor before the scene restarts
Load the World Anchor after the scene restarted
This however only works on the emulator / HoloLens itself. In the Unity Editor World Anchors don't work! So just copy paste the coordinates over like I did in my initial post.
I need to change scenes while the player press any key. it has worked perfectly, but when I addForce in Y axis and change scene, the loaded scene starts with none key pressed.
summing up: I walk and collide in gameobject and scene changes, the next scene start with the character walking, when I jump and collide in gameobject and change scenes, the next scene star white character stopped.
If you aren't already using it, use DontDestroyOnLoad on your player object, and of course don't add a new player in your new scene. That should work.
If it doesn't, I would probably try to either add new force in the new scene, maybe by somehow storing the force applied in the 1st scene in a GameHandler (that also should persist through scenes).
If that doesn't work maybe you have to consider not changing scenes, and simply moving the character to a new location in scene 1.
You haven't showed what you are doing or what you have tried so it's hard to answer really.
I'm making a 2D Shooter game in XNA. I had been working on the shooting speed of the player (Every how often can the player shoot another bullet) and made it so that the player can only shoot again once the previous bullet has traveled a certain distance like so:
if (this.bulletList[0].BULLETS[(this.bulletList[0].BULLETS.Count) - 1].X >= Pos.X + attackSpeed)
canShoot = true;
Where bulletList is the available Projectiles the player can shoot, BULLETS is the List of Bullets already shot, and attackSpeed is the rate in which the bullets should be shot, or simply put: the distance the bullet has to travel until another bullet can be shot.
Now I've been working on Collisions. My method was to dispose of a bullet after it hits a target like so:
for (int i = 0; i < player.BULLETLIST[0].BULLETS.Count; i++)
{
if (CollisionManager.PlayerBulletOnBot(player.BULLETLIST[0].BULLETS[i], bot))
player.BULLETLIST[0].BULLETS.RemoveAt(i);
}
Problem is, if the bullet has been removed upon hitting a target, I can no longer ask if that bullet has passed the given distance for another bullet to be shot.
To solve that, I'd like the bullet to turn invisible upon hit, and afterwards it'll be disposed of in another function that's already been made.
Simply have a Visible flag for a Bullet instance:
class Bullet {
public bool Visible { get; set; }
}
When it hits.. make it invisible:
// ... hit
bulletInstace.Visible = false;
Then check before drawing it:
if (bulletInstance.Visible)
drawBullet(bullet);
If it isn't visible, your drawing code should just skip over it.
When you draw an object using spriteBatch.Draw(...) you have to select a color for the sprite itself.
We all know that the color is a mixture of "red, green and blue" values (or rgb). What not many people know is that, at least in XNA, there is a fourth value called alpha.
The alpha value indicates the transparency of your object, meaning that if you use it like the code below, your object will be half invisible (or half visible).
spriteBatch.Draw(..., Color.White * 0,5f,...);
You can play with that :)
Check a little more here, on the old XNA forums.
You need rate of fire (rof) property that you increase to some maxRof value when you hold down button. and when rof is equal to maxrof then you add bullet to a list of bullets (and reset rof to 0).
Do not make it invisible, remove it from list. Every instance of bullet should have "Active" property set to true on fire. (when you fire a bullet, add it to list) And when collsition happened set this property to false. pseudo code as example:
UPDATE
for each bullet in bullets
-- update bullet position
-- check collision if happened if yes then set Active to false
end for
bullets.removeall(function(c) NOT(c.active));
DRAW
for each bullet in bullets.findall(function(c) c.active)
-- draw your bullets
end for